Java Program to reverse the words of a string


February 28, 2022, Learn eTutorial
1182

Here we are explaining how to write a java program to reverse the words of a string. So first we have to read a string from the user and split it into words. Then we have to reverse each word and display the reversed string.

How to split a string in java?

We can split a string in java by using split() method of String class. An example is shown below.

str="I am very happy"
String[] ws = str.split(" ");
 

Now the array ws contains the strings,

I  am very  happy

Here the split(" ") function splits the string str into several strings based on space. Here we passed the delimiter as space.

How to implement the java program to reverse all the words of a string?

First, we have to declare the class RString with a public modifier. Then open the main() and read the string which the user wants to reverse into the variable str. Then call the function reverseWords(str) to find out the reverse of the string.

In the reverseWord() function, first we will split the string into different words and keep it in an array. Then we will take each word from the array and reverse it. After that, we will keep the reversed word in string rs. After reversing all words, we will display the given string and the reversed string.

ALGORITHM

STEP 1: Declare the class RString with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Create an object of Rstring as obj.

STEP 4: Read a string from the user into the variable str.

STEP 5: Call the function reverseWord(str)

 

FUNCTION  public void reverseWord(String str)

STEP 1: Split the given string str into words and save it into the array ws[].

STEP 2: Declare a string rs as a reverse string.

STEP 3: By using for loop with the condition i

STEP 4: Declare the string r.

STEP 5: Assign word =ws[i] and rWord="".rWord is used to hold the reverse of the word.

STEP 6: By using a for loop with j=world.length()-1,findout rWord=rWord+word.charAt(j).Then decremnet j by one and repeate the step until j<0.

STEP 7: Assign rs=rs+rWord+""..Here rs means the reverse string.

STEP 8: Increment i by one and repeat step 3.

STEP 9: Display the given string as str.

STEP 10: Display the reverse string as rs.

 

Java Source Code

                                          import java.util.Scanner;
public class RString {
    public void reverseWord(String str) {

        String[] ws = str.split(" "); //Split the given string into words
        String rs = "";
        for (int i = 0; i < ws.length; i++) {
            String r;
            String word = ws[i];
            String rWord = ""; //Reverse of each word
            for (int j = word.length() - 1; j >= 0; j--) {

                rWord = rWord + word.charAt(j);
            }
            rs = rs + rWord + " ";
        }
        System.out.println(str); //Display the given string
        System.out.println(rs); //display the reverse string
    }
    public static void main(String[] args) {
        RString obj = new RString();
        Scanner sc1 = new Scanner(System.in);

        System.out.println("Enter the String :");

        String str = sc1.nextLine();
        obj.reverseWord(str);

    }
}
                                      

OUTPUT

Enter the String :How are you?

How are you?
woH era ?uoy