Java Program to Reverse Words in a Sentence

Java Program to Reverse Words in a Sentence : In Java we can reverse each word in a sentence using String Methods. In this program, we are usingĀ JOptionPane.showInputDialog() method of javax.swing package to accept input from user. In java inputs are String by default.

//Java Programe to Reverse Words in a Sentence

import javax.swing.*;
class StrRev
{
public static void main(String arg[])
{
String name=JOptionPane.showInputDialog("Enter a sentence");
name+=" ";
int i,k;
String revstr="",str="";
for(i=0;i<name.length();i++)
{
        str+=name.charAt(i);
        if(name.charAt(i)==' ')
        {
               for(k=str.length()-1;k>=0;k--)
	       {
		       revstr+=str.charAt(k);
	       }
	str="";
	}
}
System.out.println("Revserse of Sentence is : "+revstr);
}
}

Input  : THIS IS EXAMPLE
Output : SIHT SI ELPMAXE

Reversing words in a sentence is a common Java programming question asked in coding practice, interviews, and beginner programming exercises.

In this program, the position of words in the sentence is reversed while keeping the words readable. Java provides simple methods and string functions that make this task easier for beginners.

This article explains the Java program to reverse words in a sentence with examples, output, algorithm, and explanation.

Problem Statement

Write a Java program to reverse the words in a sentence.

Example

Input:

Java is easy

Output:

easy is Java

Java Program to Reverse Words in a Sentence

public class ReverseWordsExample {

    public static void main(String[] args) {

        String sentence = "Java is easy";

        String[] words = sentence.split(" ");

        for (int i = words.length - 1; i >= 0; i--) {
            System.out.print(words[i] + " ");
        }
    }
}

Output

easy is Java

Explanation of the Program

The program first stores the sentence in a string variable.

String sentence = "Java is easy";

The split() method separates the sentence into individual words.

String[] words = sentence.split(" ");

A loop starts from the last word and prints each word in reverse order.

for (int i = words.length - 1; i >= 0; i--)

This logic reverses the order of words in the sentence.

Algorithm to Reverse Words in a Sentence

  1. Start the program
  2. Store the sentence in a string variable
  3. Split the sentence into words using split()
  4. Start a loop from the last word
  5. Print each word in reverse order
  6. Stop the program

Why Use split() Method in Java?

The split() method is used to divide a string into multiple parts based on a separator.

In this program, space " " is used as the separator to separate words in the sentence.

Advantages of This Program

  • Simple and beginner-friendly logic
  • Easy to understand and implement
  • Helps improve string handling concepts
  • Useful for coding interviews and practice

Important Note About Sentence Reversal

This program reverses the order of words, not the individual characters inside each word.

For example:

Input:

Java is easy

Output:

easy is Java

It does not produce:

avaJ si ysae

Alternative Java Program Using StringBuilder

public class ReverseSentence {

    public static void main(String[] args) {

        String sentence = "Java programming language";

        String[] words = sentence.split(" ");

        StringBuilder reversed = new StringBuilder();

        for (int i = words.length - 1; i >= 0; i--) {
            reversed.append(words[i]).append(" ");
        }

        System.out.println(reversed.toString());
    }
}

Output

language programming Java

Frequently Asked Questions

How do you reverse words in a sentence in Java?

Words can be reversed in Java using the split() method and a loop that prints words from the last index to the first index.

Which method is used to split words in Java?

The split() method is used to divide a sentence into multiple words.

Is this program important for interviews?

Yes, reversing words in a sentence is a common Java interview and coding practice question.

Can beginners learn this program easily?

Yes, this program is simple and suitable for beginners learning Java string concepts.

Conclusion

The Java program to reverse words in a sentence is an important beginner-level string programming example. It helps students understand string handling, loops, arrays, and basic Java logic.

By using methods like split() and classes like StringBuilder, developers can easily reverse the order of words in a sentence in Java.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top