May 8, 2009

C++ Utility: Reverse a string

char* ReverseString(char* input)
{
if(!input) return 0; //null pointer

int length = static_cast<int>(strlen(input));

char* begin = input; //point to beginning of the input string
char* end = input + length - 1; //point to the ending of the input string

char temp;
while(begin < end)
{
temp = *begin;
*begin = *end;
*end = temp;
++begin; --end;
}

return input;
}

No comments:

Post a Comment