Introduction
Reversing each word in a string is a common task in text processing. This exercise helps you understand how to manipulate strings, split and reassemble them, and use loops to reverse the characters in each word. This guide will walk you through writing a Java program that reverses each word in a given string while keeping the word order intact.
Problem Statement
Create a Java program that:
- Prompts the user to enter a string.
- Reverses each word in the string.
- Displays the string with each word reversed.
Example:
- Input:
"Hello World"
- Output:
"olleH dlroW"
Solution Steps
- Read the String: Use the
Scanner
class to take the string as input from the user. - Split the String into Words: Use the
split()
method to break the string into words. - Reverse Each Word: Use a loop to reverse the characters in each word.
- Reassemble the String: Combine the reversed words back into a single string.
- Display the Result: Print the string with each word reversed.
Java Program
// Java Program to Reverse Each Word of a String // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class ReverseWordsInString { public static void main(String[] args) { // Step 1: Read the string from the user try (Scanner scanner = new Scanner(System.in)) { System.out.print("Enter a string: "); String input = scanner.nextLine(); // Step 2: Split the string into words String[] words = input.split("\\s+"); // Step 3: Reverse each word StringBuilder reversedString = new StringBuilder(); for (String word : words) { StringBuilder reversedWord = new StringBuilder(word); reversedString.append(reversedWord.reverse().toString()).append(" "); } // Step 4: Display the result System.out.println("String with each word reversed: " + reversedString.toString().trim()); } } }
Explanation
Step 1: Read the String
- The
Scanner
class is used to read a string input from the user. ThenextLine()
method captures the entire line as a string.
Step 2: Split the String into Words
- The
split()
method is used to divide the string into words based on whitespace. The regex\\s+
handles multiple spaces between words.
Step 3: Reverse Each Word
- A
for
loop is used to iterate through each word in the array. - Each word is reversed using the
StringBuilder
‘sreverse()
method. - The reversed words are appended to the
StringBuilder
objectreversedString
, with a space added after each word.
Step 4: Display the Result
- The program prints the final string, where each word is reversed but the order of the words remains the same. The
trim()
method is used to remove any trailing spaces.
Output Example
Example:
Enter a string: Hello World String with each word reversed: olleH dlroW
Conclusion
This Java program demonstrates how to reverse each word in a user-input string while keeping the word order intact. It covers essential concepts such as string manipulation, using StringBuilder
to reverse strings, and handling arrays, making it a valuable exercise for beginners learning Java programming.