Created
July 7, 2014 03:15
-
-
Save elleryq/2cd03bcca38900a78af5 to your computer and use it in GitHub Desktop.
reverse a string in C
This file contains hidden or 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 <stdlib.h> | |
| #include <string.h> | |
| #include <assert.h> | |
| char* reverse(char* s) { | |
| assert(s!=NULL); | |
| char *r = (char*)strdup(s); | |
| int len = strlen(s); | |
| if(r==NULL) { | |
| return NULL; | |
| } | |
| while(--len) { | |
| *(r+len) = *s; | |
| s++; | |
| } | |
| *r = *s; | |
| return r; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment