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…

    Java Program to Reverse a Sentence Using Recursion

    This Java example code demonstrates a simple Java program to reverse a sentence using recursion and print the output to the screen.

    Example:

    public class Reverse {
    
      public static void main(String[] args) {
        String sentence = "Welcome back";
        String reversed = reverse(sentence);
        System.out.println("The reversed sentence is: " + reversed);
      }
    
      public static String reverse(String sentence) {
        if (sentence.isEmpty())
          return sentence;
    
        return reverse(sentence.substring(1)) + sentence.charAt(0);
      }
    }

    Program Output:

    The reversed sentence is: kcab emocleW
    


    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.