Domanda di colloquio di Amazon

Write the code to reverse a string.

Risposta di colloquio

Anonimo

22 mar 2011

void reverseString(char *str) { int i=0; int j=strlen(str)-1; char temp; while(i<=j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } } This is an O(n) operation.. linear time. just exchange first and last terms and keep moving to the middle.

2