Skip to content

Instantly share code, notes, and snippets.

@chouclee
Created June 17, 2014 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chouclee/bd4d1f0292c7111bbe74 to your computer and use it in GitHub Desktop.
Save chouclee/bd4d1f0292c7111bbe74 to your computer and use it in GitHub Desktop.
[CC150][1.2] Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.
#include <iostream>
using namespace std;
void reverseString(char *str) {
char *start = str;
for (;*str;str++); //loop to '\0' terminal of the string
if (*start) str--; //in case string is blank
while (start < str) {
*start ^= *str; //swap two elements using XOR
*str ^= *start;
*start++ ^= *str--;
}
}
int main() {
char str[] = "";
cout << str << endl;
reverseString(str);
cout << str << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment