Let’s see an approach to reverse words of a given String in Java without using any of the String library function
Examples:
Input : "Welcome to geeksforgeeks" Output : "geeksforgeeks to Welcome" Input : "I love Java Programming" Output :"Programming Java love I"
Prerequisite: Regular Expression in Java
// Java Program to reverse a String // without using inbuilt String function import java.util.regex.Pattern; public class Exp { // Method to reverse words of a String static String reverseWords(String str) { // Specifying the pattern to be searched Pattern pattern = Pattern.compile("\\s"); // splitting String str with a pattern // (i.e )splitting the string whenever their // is whitespace and store in temp array. String[] temp = pattern.split(str); String result = ""; // Iterate over the temp array and store // the string in reverse order. for (int i = 0; i < temp.length; i++) { if (i == temp.length - 1) result = temp[i] + result; else result = " " + temp[i] + result; } return result; } // Driver methods to test above method public static void main(String[] args) { String s1 = "Welcome to geeksforgeeks"; System.out.println(reverseWords(s1)); String s2 = "I love Java Programming"; System.out.println(reverseWords(s2)); } } |
Output:
geeksforgeeks to Welcome Programming Java love I
You can find the c++ solution for Reverse words in a String here
This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if the given string of words can be formed from words present in the dictionary
- Program to reverse words in a given string in C++
- Reverse words in a given string | Set 2
- Reverse words in a given String in Python
- Reverse words in a given string
- Print words of a string in reverse order
- Reverse String according to the number of words
- Reverse middle words of a string
- Reverse the Words of a String using Stack
- Count words that appear exactly two times in an array of words
- Reverse individual words with O(1) extra space
- Reverse individual words
- Swap corner words and reverse middle characters
- Check if a given string is a Reverse Bitonic String or not
- Java program to print Even length words in a String
- Program to extract words from a given String
- Check if a two character string can be made using given words
- Shortest substring of a string containing all given words
- Check if given words are present in a string
- Extract and print words separately from a given Camel Case string

