Last active
August 29, 2015 14:25
-
-
Save JIElite/5c39ace2b6ed29f32543 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 *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