Skip to content

Instantly share code, notes, and snippets.

@Morse-Code
Created April 4, 2013 12:36
Show Gist options
  • Save Morse-Code/5310016 to your computer and use it in GitHub Desktop.
Save Morse-Code/5310016 to your computer and use it in GitHub Desktop.
Reverse a C string using bitwise XOR operator.
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;
}
}
@arunkumardancer
Copy link

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.

@marger
Copy link

marger commented Sep 28, 2016

@arunkumardancer, that's right

@akashdecoder
Copy link

can u please give me the explanation of line 11 12 13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment