Search code examples
crecursionreversesentence

C - Reversing a sentence - Recursion - Without Array


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

When 'c==\n', the program doesn't even get inside the 'if' block. Then how does it print the converted sentence in the end. Please explain where the 'return' happens and also to where it gets returned.


Solution

  • when 'c==\n', it will be the end of the recursion, the function will not call itself any further and return to the last call, which continues on the line of 'printf("%c", c)', thus printing the last char of the string, then return to the second last call and so on..