-2

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");
}
Daniel Logvin
  • 502
  • 7
  • 26
  • 2
    [Don't use gets](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). use `fgets()` instead. – Achal Oct 26 '18 at 16:39
  • I don't see any variables declared in a function outside of main in this code. – Christian Gibbons Oct 26 '18 at 16:39
  • `inverted` is not a variable. It's the name of a parameter. It's only visible inside the function that is receiving that parameter. It only exists from the point it's declared in `void InvertString()` to the closing brace of that function. Re-read your lessons. – Ken White Oct 26 '18 at 16:41
  • Ok, so what i need to do then is pass that `inverted` to the main to print what it is giving me the `InvertString` function. How can i actually do that? – Daniel Logvin Oct 26 '18 at 16:43
  • 1
    I feel you're probably asking the wrong question. How do you expect main to print your inverted string when it is only printing a single character? – Christian Gibbons Oct 26 '18 at 16:45
  • `printf("%c", &cadena);`->`printf("%s", cadena);` – Tormund Giantsbane Oct 26 '18 at 17:01

2 Answers2

0

In main you declare a variable of type char called inverted - this can only hold one character. You then pass it as a pointer to your function and try to use it as a string which won't work.

Instead you could declare it as an array of char, much the same way you declare string

char inverted[200];

... and then change how you're calling the function and just pass in inverted since it is now a string.

InvertString(string, inverted);

You'd then print it as normal as a string

printf("%s", inverted);

Your recursive function doesn't work because you never change the value of inverted. What you probably want is for inverted to point at the end of where your reversed string would be and track backwards.

Something along the lines of:

void InvertString(char *string, char *inverted) {
  if (*string != '\0') {
    *inverted = *string;
    InvertString(string+1, inverted-1);
  }
}

int main(int argc, char const *argv[]) {
  char string[200];
  char inverted[200] = { 0 };
  int i;
  printf("Give me a phrase: ");
  fgets(string,200,stdin);
  printf("Tu frase invertida es: ");
  InvertString(string, inverted+strlen(string)-1);
  printf("%s", inverted); /* Here I'm tring to print "inverted", but i don't know how to pass that variable to the main */
  printf("\n");
}
Chris Turner
  • 8,082
  • 1
  • 14
  • 18
  • Ok, but when i do that, what happens is that if i put for example "hello", i only get the `h`, but i need to get `olleh` on the `printf`, how can i do that? – Daniel Logvin Oct 26 '18 at 16:48
  • That's cos your recursive routine isn't right either...bit long for a comment, so I'll tack on what is wrong in the answer – Chris Turner Oct 26 '18 at 16:50
  • I mean, the recursion does it's job when i do `printf("%s", invertido);` inside of it, it does print the word inverted. And i want to get the whole inverted word to the main and print it there. – Daniel Logvin Oct 26 '18 at 16:56
  • Your function doesn't change the value of `inverted` though, so how would it ever change anything except for the first character? – Chris Turner Oct 26 '18 at 17:00
  • I edited, the post, check it out. Now what i get is `olleh'` with that little `'` – Daniel Logvin Oct 26 '18 at 17:00
  • That isn't reversing the string - it's just printing out the original string backwards – Chris Turner Oct 26 '18 at 17:01
  • That's actually what i want. Get the original string printed backwards, store it in a variable and print that variable in the main. – Daniel Logvin Oct 26 '18 at 17:02
0

Solved:

#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);
  for(i=0;i>=strlen(cadena);i++){
  printf("%c", cadena[i]);
  }
  printf("\n");
}
Daniel Logvin
  • 502
  • 7
  • 26
  • If you use this technique then what is the point of using recursing??? Just print the string in reverse order? Check this line `for(i=0;i>=strlen(cadena);i++)` properly. This loop will never execute. – suvojit_007 Oct 26 '18 at 17:18