Created
April 4, 2013 12:36
-
-
Save Morse-Code/5310016 to your computer and use it in GitHub Desktop.
Reverse a C string using bitwise XOR operator.
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 stringReverse(char *str) | |
{ | |
char *p1, *p2; | |
if (!str || !*str) | |
{ | |
NSLog(@"No string passed into reverse function."); | |
} | |
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) | |
{ | |
*p1 ^= *p2; | |
*p2 ^= *p1; | |
*p1 ^= *p2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks .. nice code. As per my understanding the code inside for loop is swapping two characters using XOR operator. Let me know, If my understanding is not correct.