Skip to content

Instantly share code, notes, and snippets.

@zachelko
Created April 8, 2010 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachelko/359630 to your computer and use it in GitHub Desktop.
Save zachelko/359630 to your computer and use it in GitHub Desktop.
Reversing a string the good way
void swap(char str[],
const int firstIndex,
const int secondIndex)
{
char temp = str[firstIndex];
str[firstIndex] = str[secondIndex];
str[secondIndex] = temp;
}
void reverse2(char src[])
{
//start at beginning and end
int firstIndex = 0;
int secondIndex = strlen(src) - 1;
while (secondIndex > firstIndex)
{
swap(src, firstIndex, secondIndex);
//move indices inward
++firstIndex;
--secondIndex;
}
printf("reverse2: %s\n", src);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment