Last active
August 29, 2015 14:25
-
-
Save JIElite/a8bea3448e30042cc54f to your computer and use it in GitHub Desktop.
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<string.h> | |
void swap(char *c1, char *c2){ | |
*c1 ^= *c2 ^= *c1 ^= *c2; | |
} | |
/* 想法 */ | |
char *reverse_iterative(char *s){ | |
int len = strlen(s); | |
int i; | |
for ( i = 0; i < len / 2; i++){ | |
swap(s+i, s+(len-1-i)); | |
} | |
return s; | |
} | |
char *reverse_recursive(char *s){ | |
static int time = 0; | |
static int len = 0; | |
if (time == 0){ | |
len = strlen(s); | |
} | |
if (time == len / 2){ | |
return s; | |
} | |
swap(s+time, s+len-1-time); | |
time += 1; | |
return reverse_recursive(s); | |
} | |
int main(){ | |
char str[10] = "hello"; | |
// printf("%s\n", reverse_iterative(str)); | |
reverse_recursive(str); | |
printf("%s\n", reverse_recursive(str)); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment