Created
April 8, 2010 00:37
-
-
Save zachelko/359629 to your computer and use it in GitHub Desktop.
Reversing a string the bad way
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 reverse(char str[]) | |
{ | |
// Add 1 for the terminating character | |
int len = strlen(str) + 1; | |
char temp[len]; | |
// Data resides from indices 0-2 | |
int i = len - 2; | |
for (; i >= 0; --i) | |
{ | |
// Copy front to back | |
temp[i] = str[len - (i + 2)]; | |
} | |
// Add terminating character | |
temp[len - 1] = '\0'; | |
// Copy into original | |
strncpy(str, temp, len); | |
printf("reverse: %s\n", str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment