Skip to content

Instantly share code, notes, and snippets.

@BassyKuo
Created January 22, 2017 08:35
Show Gist options
  • Save BassyKuo/2632dd8907a679f43516eed850f2607b to your computer and use it in GitHub Desktop.
Save BassyKuo/2632dd8907a679f43516eed850f2607b to your computer and use it in GitHub Desktop.
Reverse using in-place & recursive
#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