Write a java program to reverse each word of a string without changing the order of the word..
Suppose, If an input string is Java Programming then the output string should be avaJ gnimmargorP.
Example –
Input: Java Programming
Output: avaJ gnimmargorP
In this tutorial, I am going to explain multiple approaches to solve this problem.
Java Program to Reverse Each Word of a String
The easiest way is to split the input string into array of words is by using split() method. The string split() method splits a given string around matches of the given regular expression. After splitting, it returns a String array. In this example, we are splitting the string around space.
Here are the steps to solve this problem –
i) Split the string using split() method. Once string is split, we get array of words.
ii) Traverse an array and take each word at a time.
iii) Reverse a word and append to the final string.
iv) Finally, print or return the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Reverse each word using split public class ReverseEachWords { public static void main(String[] args) { String str = "Java Programming"; //Create an array of words //[Java,Programming] String[] words = str.split(" "); String revString = ""; for(int i = 0; i < words.length; i++) { String word = words[i]; String revWord = ""; for(int j = word.length() - 1; j >= 0; j--) { revWord = revWord + word.charAt(j); } revString = revString + revWord + " "; } System.out.println(revString); } } |
Find duplicate characters in a string
Check whether two strings are anagram of each other
Reverse Each Word In String without using Inbuilt Functions
In this example, i am going to use stack to solve this problem.
We can also reverse words of a string using stack data structure. In a stack, the element which we push at last is the first element to be popped out (LIFO). We can use this property of a stack to solve this problem.
Implement queue using two stacks
Here are the steps –
i) Declare a stack which store the value of character type.
ii) Traverse a string and push each character in a stack until the space is encountered. When space is encountered popped all the characters from a stack unitl it is empty. Append the reverse words in a string.
The time and space complexity of this approach is O(n).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
//Reverse words using stack import java.util.Stack; public class ReverseEachWord { public static void main(String[] args) { String str = "Java Programming"; String revString = ""; Stack<Character> st = new Stack<>(); for(int i = 0; i < str.length(); i++) { //If character is not a space push them in a stck if(str.charAt(i) != ' ') { st.push(str.charAt(i)); } else { //If it's a space, then reverse prev word revString = reverseWord(revString, st); } } revString = reverseWord(revString, st); System.out.println(revString); } private static String reverseWord(String revString, Stack<Character> st) { String revWord = ""; while(!st.isEmpty()) { revWord = revWord + st.pop(); } revString = revString + revWord + " "; return revString; } } |
Reverse Each Word in a String in Java 8 Using Lambda
In this example, i am going to write a code to solve this problem using Java 8 lambda.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.stream.Collectors; //Reverse using lambda public class ReverseEachWord { public static void main(String[] args) { String str = "Java Programming"; String result = Arrays.asList(str.split(" ")) .stream() .map(s -> new StringBuilder(s).reverse()) .collect(Collectors.joining(" ")); System.out.println(result); } } |