Sunday, December 26, 2010

Reverse a string with O(1) additional space

Old interview question

1 comment:

  1. no additional space:

    void reverse(char str[]){
    int i,size = strlen(str);
    for (i = 0; i < size/2; i++){
    str[i] = str[i] ^ str[size - i - 1];
    str[size - i - 1] = str[i] ^ str[size - i - 1];
    str[i] = str[i] ^ str[size - i - 1];
    }
    return;
    }

    ReplyDelete