Created
January 17, 2016 12:51
-
-
Save krysseltillada/e0a585bbed06dbd15bc6 to your computer and use it in GitHub Desktop.
ex 4-13 (c the programming language 2 ed) reverse recursion
This file contains hidden or 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 reverse (char str[]) | |
{ | |
static char temp[100]; | |
static int i = 1; | |
static int first = 0; | |
static int last; | |
last = strlen (str) - i; | |
temp[first++] = str[last]; | |
++i; | |
if (last <= 0) { | |
temp[first] = '\0'; | |
strcpy (str, temp); | |
return; | |
} | |
reverse (str); | |
} | |
int main () | |
{ | |
char str [] = {'h', 'e', 'l', 'l', 'o', '\0'}; | |
reverse (str); | |
printf ("%s ", str); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment