4

Recursion. I checked other online solutions and they seem to be pretty much identical to mine. The code should reverse the string (in its position) but it does not. E.g when input is st2 = "abcdefg" the output is an empty string. I was expecting st2 = "gfedcba". What am i missing?

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


void recurse_reverse(char s[], int sz)
{
    int i=0,j = sz -1;
    if(i<j)
    {
        swap(&s[i],&s[j]);
        recurse_reverse(s+1, sz-2);

    }
}


void swap( char* s1, char *s2)
{
    char tmp;
    tmp = *s1;
    *s1 = *s2;
    *s2 = tmp;
}


int main(void)
{
    char st1[9] = "abcdefg", st2[9];
    strcpy(st2,st1);
    recurse_reverse(st2,9);
    printf("s1 = %s\ns2 = %s",st1,st2);
    printf("\n" );
    return 0;
}
3
  • 2
    You should pass strlen(st2) to recurse_reverse. Commented Nov 16, 2016 at 10:32
  • 1
    One of the first chars you swap is the '\0' which becomes the end of the string. Commented Nov 16, 2016 at 10:34
  • The only purpose of st1 is to display the old value.Delete please st1 and review the code, i did not expect it would confuse you. The function prototype of recurse_reverse supposed to be : void recurse_reverse(char s[], int size); Let's say that st1[9] does not exist, strcpy() does not exist and st2[9] ="abcdefg", which i want to be reversed. Thank you. Commented Nov 16, 2016 at 11:21

3 Answers 3

5

You are swapping the 2 zero bytes at the end of st1. Hence, the st2 starts with a null byte and thus the printf() isn't printing anything. You just need to fix your argument passing. Instead of

recurse_reverse(st2,9);

do

recurse_reverse(st2,strlen(st1));

You probably want to add logic to make sure your destination array st2 has sufficient space.

Sign up to request clarification or add additional context in comments.

3 Comments

@Jean-FrançoisFabre C standard uses the term "null character" to denote '\0'. So, I don't see any issue with calling it "null byte" or any ambiguity.
please check my comment right below my code. I should have placed it here, but i made a mistake. Also, after seeing your response ,by deductive reasoning i typed recurse_reverse(st2,strlen(st2)); and worked like a charm.
@usr duly noted, I never know how to call it. At least not NULL. at least that answer is good but I was angry at the 2 other ones. I just wanted to know how it would turn out.
3

I added a printf statement to debug the issue and got the below output. You are trying to access 9th variable which is a terminated null character \0 hence you only get \0 as output and not the actual reversed string.

Instead of hardcoding the size of string you can use strlen to get the string length.

1st char = a and 9th char is ▒
1st char = b and 9th char is
1st char = c and 9th char is g
1st char = d and 9th char is f
s1 = abcdefg
s2 = ▒

Solution

Intended code change

recurse_reverse(st2,strlen(st1));

Output

1st char = a and 9th char 9th char is g
1st char = b and 9th char 9th char is f
1st char = c and 9th char 9th char is e
s1 = abcdefg
s2 = gfedcba

Comments

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

void swap( char* s1, char *s2);

void recurse_reverse(char s[], int sz)
{
    int i=0,j = sz-1;
    if(i<j)
    {
        swap(&s[i],&s[j]);
        recurse_reverse(s+1, sz-2);

    }
}


void swap( char* s1, char *s2)
{
    char tmp;
    tmp = *s1;
    *s1 = *s2;
    *s2 = tmp;
}


int main(void)
{
    char st1[9] = "abcdefg", st2[9];
    int len=0;
    strcpy(st2,st1);
    len =strlen(st2);
    recurse_reverse(st2,len);
    printf("s1 = %s\ns2 = %s",st1,st2);
    printf("\n" );
    return 0;
}

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.

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.