Skip to content

Instantly share code, notes, and snippets.

@zachelko
Created April 8, 2010 00:37
Show Gist options
  • Save zachelko/359629 to your computer and use it in GitHub Desktop.
Save zachelko/359629 to your computer and use it in GitHub Desktop.
Reversing a string the bad way
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