Browsing the blog archives for January, 2010

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 […]