I've just started with recursions and i wanted to pass a variable that is declared in a function to the main.
Here is the code to help you understand it a little bit better:
#include <stdio.h>
void InvertString(char string[200], char *inverted) {
if (*string != '\0') {
InvertString(string+1, inverted);
}
*inverted = *string;
}
int main(int argc, char const *argv[]) {
char string[200];
char inverted;
int i;
printf("Give me a phrase: ");
gets(string);
printf("Tu frase invertida es: ");
InviertString(string, &inverted);
printf("%c", inverted); /* Here I'm tring to print "inverted", but i don't know how to pass that variable to the main */
printf("\n");
}
Thank you for your help.
What about this?
#include <stdio.h>
#include <string.h>
void InvierteFrase(char cadena[200]) {
if (*cadena != '\0') {
InvierteFrase(cadena+1);
}
printf("%c", *cadena);
}
int main(int argc, char const *argv[]) {
char cadena[200];
char invertido[200];
int i;
printf("Dame una frase: ");
gets(cadena);
printf("Tu frase invertida es: ");
InvierteFrase(cadena);
printf("%c", &cadena);
printf("\n");
}