Created
April 8, 2010 00:39
-
-
Save zachelko/359630 to your computer and use it in GitHub Desktop.
Reversing a string the good 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 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