Created
February 7, 2013 00:21
-
-
Save linusthe3rd/4727217 to your computer and use it in GitHub Desktop.
in-place String reversal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void strrev(char *p) { | |
char *q = p; | |
while(q && *q) ++q; //get last char of the string | |
for(--q; p < q; ++p, --q) { | |
*p = *p ^ *q, //xor p | |
*q = *p ^ *q, // xor np w/ q to store p's value in q | |
*p = *p ^ *q; //xor np w/ nq to get q's original value in p | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment