Created
July 11, 2012 18:48
-
-
Save lexich/3092329 to your computer and use it in GitHub Desktop.
revert C-string without temp
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
#include <string.h> | |
#include <stdio.h> | |
/* | |
revert buffer C-string without temp | |
abc\0 -> cba\0 | |
@str buffer | |
@len size of buffer | |
*/ | |
void revert(char* str, int len ){ | |
for( int i = 0, e = len - 2; i < e; ++i, --e ){ | |
*(str + i) ^= *(str + e); | |
*(str + e) ^= *(str + i); | |
*(str + i) ^= *(str + e); | |
} | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
char testString[] = "Test string"; | |
int len = sizeof(testString)/sizeof(char); | |
char* str = new char[len]; | |
strcpy(str, testString); | |
printf("%s\n", str); | |
revert(str, len); | |
printf("%s\n", str); | |
delete [] str; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
g++ -Wall -pedantic -O3 -o exch revert.cpp && revert