Browsing the archives for the c tag

Reverse the Order of Words in a String

Question: Reverse the order of words in a string in C using as little additional memory as possible. Answer: The solution for this questions uses the solution of Reverse a String. I am going to make a small change to that solution by adding a length parameter. It will probably be better for you to […]

Reverse a String

Question: Reverse a string in C using as little additional memory as possible. Answer: The first solution needs the┬ásize of a char and size of two integers, all of which will be allocated from the stack. This solution is the most commonly accepted “good” solution. Here is the code. void reverseString(char* str) { int i, […]

Power of 2

Question: How do you find out if an unsigned integer is a power of 2? Answer: The trick here is to know that an unsigned integer which is a power of 2 has only one of its bits as 1. So a simple solution would be to loop through the bits and count the number […]