Frequently Asked Java Program 18: Java Program to Reverse Every Word of a String

Hello Programmers,

“Java Program to Reverse Every Word of a String” is frequently asked programming interview questions to freshers and experienced. I will try here to explain logic in simple steps so that you do not need to remember lines of code. Just implement the logic and you can code it. 

Problem statement:

Original string                      :Make Selenium Easy
String with reversed words  :ekaM muineleS ysaE

We need to reverse a given string word by word. Note here we are not changing default position of words here.

Solution: 

Step 1: First of all we need to extract words from a given string. Two words are separated by while space. We can use split() method given by String class which splits a string by provided delimiter and returns a String[] of splitted words. For example:

[java]
public class SplitString {

public static void main(String[] args) {

String inputString = "Make Selenium Easy";
String spliited[] = inputString.split(" ");
for(int i=0;i<spliited.length;i++)
{
System.out.println(spliited[i]);
}

}
}
[/java]

Output:
Make
Selenium
Easy

Step 2: We have extracted words from given string in above step. To reverse a word, we just need to iterate it from last index of word, read and append it until you reach first index of word. To read any character at specific index, we have a method named chatAt(int index).  For example:

[java]
public class readCharOfString {

public static void main(String[] args) {

String inputString = "Selenium";
System.out.println(inputString.charAt(3));

}
}
[/java]

Output:
e

Because index starts from zero.

Step 3: We have extracted words and know the logic to reverse a word. You just need to call reversing word method for each word in given string. For this you can use a for loop.

Complete Java Programs:

[java]
package StringPrograms;

import java.util.Scanner;

public class ReverseEveryWordOfGroupOfString {

/*
* This method splits given string by space delimiter and returns a String
* array.
*/
public static String[] splitStringBySpace(String inputStringToSplit) {
return inputStringToSplit.split(" ");
}

/*
* This method reverse the given word and return it.
*/
public static String reverseWord(String inputWord) {
String reverse = "";
// Reading char by char from end and appending
for (int i = inputWord.length() – 1; i >= 0; i–) {
reverse = reverse + inputWord.charAt(i);
}
return reverse;
}

public static void main(String[] args) {

// User input for the string to know length
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the string to reverse each word:");

String userInput = sc.nextLine();

sc.close();

// Split string
String[] splitedString = splitStringBySpace(userInput);

// Output with each word reversed
String desiredString="";

// Reversing word by word and appending
for(int j=0;j<splitedString.length;j++)
{
desiredString= desiredString + reverseWord(splitedString[j]) +" ";
}

System.out.println("Original string :"+userInput);

System.out.println("String with reversed words :"+desiredString);
}
}

[/java]

Output:

Hopefully, you got the logic to solve above problem. If you have any doubt, kindly comment.

#HappyCoding

2 thoughts on “Frequently Asked Java Program 18: Java Program to Reverse Every Word of a String”

  1. public class RevString {

    public static void main(String[] args) {
    String str=”Make Selenium Easy”;

    String rev=””;

    char[] charArray = str.toCharArray();
    int CharA = charArray.length;

    for (int i=CharA-1 ; i>=0 ;i–) {
    //System.out.println(charArray[i]);
    rev=rev+charArray[i];

    }
    System.out.println(rev);
    }
    }

    Output: ysaE muineleS ekaM

Leave a Reply

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