Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    C Program to Reverse a Sentence Using Recursion

    C Programming Examples


    This C program takes a input from user and reverses that sentence using recursion.

    Program:

    #include <stdio.h>
    
    void reverse();
    
    void main()
    {
        printf("Please enter a sentence: ");
        reverse();
    }
    
    void reverse()
    {
        char c;
        scanf("%c", &c);
        if (c != '\n') {
            reverse();
            printf("%c", c);
        }
    }

    Program Output:

    reverse-sentence-recursion

    Explanation:

    This program prints "Please enter a sentence: " in screen and then takes input from the user.
    reverse() function called and function stores the first letter entered by user and stores in variable c. If that variable is other than '\n' [enter character] then, again reverse() function is called. Then, the second character is stored in variable c of second reverse function. This process goes on until the user enters '\n'.

    When, user enters '\n', the last function reverse() function returns to second last reverse() function and prints the last character. Second last reverse() function returns to the third last reverse() function and prints second last character. This process goes on and the final output will be the reversed sentence.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.