Skip to content

Instantly share code, notes, and snippets.

@JIElite
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JIElite/a8bea3448e30042cc54f to your computer and use it in GitHub Desktop.
Save JIElite/a8bea3448e30042cc54f to your computer and use it in GitHub Desktop.
#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