2

I am currently studying C and I can't get past this exercise. I must create a recursive function to reverse string1 into string2. Here is my code. I would gladly appreciate your help.

#include <stdio.h>
#define MAX 100

void reverse(char s1[],char s2[],int n,int j);

int main()
{
    char string1[MAX]="How Are You Mate";
    char string2[MAX]="";
    int n=0;
    int i=0;
    int j=0;

    for(i=0;string1[i]!='\0';i++)
        n++;
    reverse(string1,string2,n,j);
    printf("String-a normal:\n%s\n",string1);
    printf("String-a reverse:\n%s\n",string2);
    return 0;
}

void reverse(char s1[],char s2[],int n,int j)
{
     if(n>0)
     {
            s2[j]=s1[n];
            reverse(s1,s2,n-1,j+1);
     }
     else
            s2[j]='\0';
}
4
  • The code does compile fine,the problem is it doesnt show any characters when I print the 2 string. Commented Mar 10, 2013 at 11:29
  • s2[] stands for the second string Commented Mar 10, 2013 at 11:34
  • @Lind, does the exercise mention any benefit you could gain from using a recursive function to reverse a string in C, whose "strings" cannot be returned easily and therefore do not fit that pattern very well? Commented Mar 10, 2013 at 11:35
  • It says the same thing as you said.Using a recursive function does not make a faster program,but still it asks for an exercise with a recursive function.I cant move on without solving this exercise. Commented Mar 10, 2013 at 11:37

5 Answers 5

5

in-place (the caller could make a copy of the string before calling this function) string reverse with tail-recursion

void reverse (char *str, size_t len)
{
  char tmp;
  if (len-- < 2) return;

  tmp = *str;
  *str = str[len];
  str[len] = tmp;

  reverse (str+1, len -1);
}

O, if you don't want pointers:

void reverse (char str[], size_t len)
{
  char tmp;
  if (len-- < 2) return;

  tmp = str[0];
  str[0] = str[len];
  str[len] = tmp;

  reverse (str+1, len -1);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I fixed my original solution,and yes I understand it,although I must read pointers again cuz I don't have a clear idea on them.
3

The reversing starts by copying the n-th character of string1 array into string2. The n-th character happens to be the null terminator. It becomes the first character of your new string, so the string looks empty to all standard C routines, including printf.

Calling

reverse(string1,string2,n-1,j);

from the main should fix the problem. The condition in the reverse should be changed from if(n>0) to if(n>=0) as well.

3 Comments

Thanks this is the answer I was looking for.I made the condition string1[i]!='\0' in the for loop,but I started counting from the first character,and I think thats the problem here.
I didnt put n-1 on the function but I declared int n=-1.As for the n>=0 that was necessary thanks.
@Lind Starting n at -1 would work too. By the way, you can drop the declaration of j from main, because it is assigned zero, and is not changed after that, so you can pass 0 as your last parameter of reverse for the same effect.
2

Although it does not save the resulting string anywhere, you get the idea.

#include <stdio.h>

void rev (const char* str);

int main () {
    const char str[] = "!dlrow ,olleH";

    printf("%s\n", str);

    rev(str);
    printf("\n");

    return 0;
}

void rev (const char* str) {
    char c = *str;
    if (c != '\0') {
            rev(str + 1);
        printf("%c", c);
    }
}

Comments

1

I have corrected the program. Please find the changes below

void reverse(char s1[],char s2[],int n,int j)
{
 if(n>0)
 {
        s2[j]=s1[n-1];
        reverse(s1,s2,--n,++j);
 }
 else
        s2[j]='\0';
}

Comments

0

i recommend using library , size=strlen(array) in stead of

for(i=0;string1[i]!='\0';i++)
n++;

to count how many characters in arra

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.