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/5c39ace2b6ed29f32543 to your computer and use it in GitHub Desktop.
Save JIElite/5c39ace2b6ed29f32543 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 *rev(char *s){
static char *tail = NULL;
static char *head = NULL;
if (tail == NULL){
head = tail = s;
// move tail to end of string.
while(*tail++);
tail -= 2;
}
if (head - tail >= 0){
return s;
}
// Maybe we can detect palindrome.
swap(head++, tail--);
return rev(s);
}
int main(){
char str[] = "hello";
printf("%s\n", rev(str));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment