1

I am facing this problem I can't get it to work

#include <stdio.h>
#include <string.h>

void RecursiveReverse(char word)
{
    if (word == '\0')
        return;

    RecursiveReverse(word + sizeof(word));

    printf("%c", word);
}

int main(void)
{
    printf("enter a word please =>"); 
    char toBeRev;
    scanf("%s", toBeRev); 

    RecursiveReverse(toBeRev);

    printf("\n");
}

I am supposed to request for a word, and send it to a function to get it reversed.

yhyrcanus
  • 3,743
  • 2
  • 23
  • 28
aPEARR
  • 53
  • 1
  • 10

4 Answers4

3

First mistake you do something like:

char toBeRev; 
scanf("%s", toBeRev); 

so you try to fill toBeRev with the user input but %s take a char* and not char

So you must have a buffer who can contain the input of the user.

char input[4096] = {0};

Then you say that you just need to print the string in a reverse order, so you don't need to change the value of your string, and you started with a recursive function (which is a good idea)

I have done something according to your exemple

  void reverse(const char *str) //you don't need to modify your string
   { 
      if (*str != '\0') //if the first character is not '\O' 
        reverse((str + 1)); // call again the function but with +1 in the pointer addr
      printf("%c", *str); // then print the character
    }

int main()
{
  char input[4096] = {0};

  printf("Enter a word please => ");
  scanf("%s", input);
  reverse(input);
  printf("\n");
  return (0);
}

so if the input is 'Hi', in the input you will have ['H']['I']['\0']

first call to reverse the string is ['H']['I']['\0'] second call the string will be ['I']['\0'] tird call ['\0'] and then you print the first character of the string so IH

Alexis
  • 2,149
  • 2
  • 25
  • 39
0

Example implementation:

void reverse(char str[]) {    
    int i;
    char c;
    int len = strlen(str);

    for (i=0; i< len/2; i++) {
        c = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = c;
    }
}
Knight of Ni
  • 1,780
  • 3
  • 20
  • 47
  • what is the meaning of strlen(str)? I dont quite get the for part sorry. I am very new to c programming. – aPEARR Jul 13 '13 at 14:07
  • a correct 'c' style string is ended with '\0'. strlen() simple counts how many characters are in the table before this '\0'. strlen is part of the library string.h (you have the header already included). – Knight of Ni Jul 13 '13 at 14:12
  • for the c part, that means the string of word for eg apple is stored in str[i]? could you explain step by step how the for loop works? – aPEARR Jul 13 '13 at 14:19
  • apple is something like this { 'a' , 'p' , 'p' , 'l' , 'e' , '\0' }, this is why the strlen can recognize a number of characters in the 'str'. In for loop you go via half of the string and exchange the characters with other half :) – Knight of Ni Jul 13 '13 at 14:25
  • when u str[i] it will be { 'a' , 'p' , 'p' , 'l' , 'e' , '\0' } then it will become { 'a' , 'p' , 'p' , 'l' , '\0' } ? – aPEARR Jul 13 '13 at 14:40
  • str[i] refers to one character ( str[0]='a' , str[1]='p' .... ) . 'i' is an offset inside of 'str' table. – Knight of Ni Jul 13 '13 at 15:01
0
#include <stdio.h>
#include <string.h>

void RecursiveReverse(char* word, int len) {
    printf("%c", *word);

    if(len <= 0)
        return;

    RecursiveReverse(--word,--len);
}

int main(int argc, char** argv) {
    char toBeRev[64];
    printf("Enter a word please => ");
    scanf("%s", toBeRev);

    RecursiveReverse(toBeRev + strlen(toBeRev), strlen(toBeRev));

    return 0;
}

The above should do what you need.

0

string reverse recursive yet ... :

void StrReverse (char *str)
{
    if(*str)
    {
        StrReverse(str+1);
        putchar(*str);
    }
}