Created
February 15, 2011 04:33
-
-
Save johnpena/827096 to your computer and use it in GitHub Desktop.
Reverse a string in C without using temporary storage
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> | |
void reverse(char* str) { | |
int i; | |
int len = strlen(str); | |
for (i = 0; i < len/2 ; ++i){ | |
str[len] = str[i]; | |
str[i] = str[len - i - 1]; | |
str[len - i - 1] = str[len]; | |
} | |
str[len] = '\0'; | |
} | |
int main() { | |
char str[] = "A string"; | |
printf("String: \"%s\"\n", str); | |
reverse(str); | |
printf("And its reverse: \"%s\"\n", str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment