Created
January 22, 2017 08:35
-
-
Save BassyKuo/2632dd8907a679f43516eed850f2607b to your computer and use it in GitHub Desktop.
Reverse using in-place & recursive
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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
char* reverse (char* str) { | |
int len = strlen (str); | |
if (len < 1) return 0; | |
int tmp = *(str); | |
*str = *(str + (len-1) * sizeof(char)); | |
*(str + (len-1) * sizeof(char)) = '\0'; | |
reverse (str + 1 * sizeof(char)); | |
*(str + (len-1) * sizeof(char)) = tmp; | |
return str; | |
} | |
int main () { | |
char s[] = "apple"; | |
reverse (reverse(reverse(s))); | |
printf ("%s\n", s); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment