Google Interview Question

Write a C procedure to reverse a string in-place.

Interview Answers

Anonymous

Oct 12, 2010

void reverse(char* s) { char* back = s + strlen(s) - 1; for (; s < back; ++s, --back) { char temp = *back; *back = *s; *s = temp; } }

1

Anonymous

Nov 4, 2010

void reverse (char* str) { char* back = str + strlen(str) - 1; while (str < back) { *str = *str ^ *back; *back = *back ^ *str; *str = *str ^ *back; str++; back--; } } balla!

1

Anonymous

Oct 7, 2010

since it's a very simple question, they probably wanted more optimized code, with fewer additions/subtractions.

Anonymous

Sep 24, 2010

void reverseInPlace(char * c) { char tmp; int ptr = 0; int cLen = strlen(c); for(ptr=0; ptr