1

Can anyone help me understand how this string reversing works ?

#include <stdio.h>
void reverse();

int main()
{
    printf("Enter a sentence: ");
    reverse();

    return 0;
}

void reverse()
{
    char c;
    scanf("%c", &c);

    if( c != '\n')
    {
        reverse();
        printf("%c",c);
    }
}

In this code the function reverse is capable of getting the input sentence one character at a time, right ? And if it is not '\n' it calls the reverse function again. so the next time when the second character is taken, the second character will be in variable c, right ?

If so, how is this code able to reverse any string that is given ? And what will be the final value in c ?

4
  • 7
    This function doesn't reverse a string, it merely echos standard input in reverse by carefully placing the recursive call before the print statement. Commented Mar 2, 2017 at 8:07
  • 1
    Now, it's time to learn how to use a debugger which lets you step through the code and allows you to inspect variables. But a piece of paper and a pencil should be enough to figure out how this works. Commented Mar 2, 2017 at 8:15
  • I swear their is a question on reversing a string everyday on this site. Commented Mar 2, 2017 at 8:40
  • @RoadRunner - Kind of like there's a question about sorting arrays in every job interview :) Commented Mar 2, 2017 at 10:30

2 Answers 2

6

The key to understanding this is that the char c variable is local to the function. That means that it is allocated on the stack each time the function is entered. On the second and subsequent calls new instances of the variable are created on the stack. These are unique and do not interfere with one-another.

When the terminating character \n is finally seen the function returns to the line after it was called and the stack is restored to the state that it had before the function was called. This means that the variable char c will be restored to it's previous value. The stack is therefore unwound with the characters being printed in reverse order of entry.

Once reverse() returns to main() there will be no final value for c because the stack frame that contained it will have been destroyed.

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

1 Comment

Thanks alot @mhawke for explaining this.
2

In this program when a string(a block of characters, say "hello") is entered , for each single character every-time the function reverse() is called and update the stack(push) unless it doesn't get Null. So, as much as the function will be called in recursive manner it'll update the stack and push down the last entered character data. At last when it get Null it'll start fetching data from stack in LIFO(Last in, first out) order. So, the last entered character will be fetched out first, second last will be second. This will happen till the first character entered. Check the image on the right corner on wikipedia to understand the operation of Stack. https://en.wikipedia.org/wiki/Stack_(abstract_data_type)

1 Comment

Thanks alot @utpal Shankar for explaining this concept to me.

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.