Quantcast
Viewing latest article 6
Browse Latest Browse All 10

How to reverse a C-Style String Interview Question

Q. Write code to reverse a C-Style String  (C-String means that “abcd” is represented as five characters, including the null character )

A.)

This is a classic interview question   The only “gotcha” is to try to do it in place, and to be care-ful for the null character

void reverse(char *str) {
   char * end = str;
   char tmp;
   if (str) {
     while (*end) {
       ++end;
     }
     --end;
     while (str < end) {
        tmp = *str;
        *str++ = *end;
        *end-- = tmp;
      }
    }
  }

Viewing latest article 6
Browse Latest Browse All 10

Trending Articles