Reverse The Position of Words in a String in Java
Example 1
Input: Hello, Everyone
Output: Everyone, Hello
Example 2
Input: This is Java Program
Output: Program Java is This
To reverse the position of words in a string, we can perform in the following ways:
By Using the split() Function
The split function in Java enables us to divide the string. Using the delimiter as a guide, we utilize it to reverse the string's split and store each line in a separate variable.
- Utilizing the String. split() method, tokenize each word, and produce an array of words.
- Reverse the order of the words as you loop through them.
ReverseWords.java
// importing the required packages
// util package is imported for Scanner class
// Scanner class is used to take inputs during the run time
import java.util.*;
import java.io.*;
public class ReverseWords {
// Main section of the program from where execution begins
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// creating object for scanner class using the key word new
System.out.println(" Enter the string ");
// Enter the string to reverse the positon of the words
String str = sc.nextLine();
System.out.println(" The original string is ");
// Splitting each word of the string using split() function
// split() function takes an delimeter and divides the string after that delimeter into new word and store it into character array
String s1[] = str.split(" ");
String reversed = "";
// reversed string variable to store the result after reversing the position of the words
// Using for loop to traverse from the end of the character array and adding it into the result
for (int j = s1.length - 1; j >= 0; j--) {
reversed += s1[j] + " ";
}
System.out.println(" The reversed string is ");
// substring() function uses the index values specified as an input, extracts a substring from the given string
// Printing the result
System.out.println(reversed.substring(0, reversed.length() - 1));
}
}
Output:
Enter the string
Hello Everyone
The original string is
Hello Everyone
The reversed string is
Everyone Hello
By Using Array
Two pointers are initialized: one at the beginning and one at the end of the string. The string is then inverted by switching the words at the beginning and finish.
Implementation
- Convert the word-containing string s into an array of string expressions.
- The left and right pointers should now be initialized to 0 and s.length() - 1.
- The elements at the left and right points must now be switched. Change the items at each place while moving the left pointer forward and the right pointer backward by one position, ensuring that the left pointer does not go beyond the right pointer.
- Give back the last string.
ReverseWords2.java
// importing the required packages
// util package is imported for Scanner class
// Scanner class is used to take inputs during the run time
import java.util.*;
public class ReverseWords2 {
// static method to perform the reverse function
public static String Reverse(String[] words) {
// integer variables are used to point the string at both ends
// One integer variable l at starting of the string
// Other integer variable r at right side end of the string
int l = 0, r = words.length - 1;
// using while loop to traverse upto mid point
while (l <= r) {
String temp = words[l];
words[l] = words[r];
words[r] = temp;
l += 1;
r -= 1;
}
String ans = String.join(" ", words);
return ans;
}
// Main section of the program from where execution begins
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// creating object for scanner class using the key word new
System.out.println(" The original string is ");
// Enter the string to reverse the positon of the words
String s = sc.nextLine();
// Splitting each word of the string using split() function
// split() function takes an delimeter and divides the string after that delimeter into new word and store it into character array
String[] words = s.split("\\s");
// reversed string variable to store the result after reversing the position of the words
String reversed = Reverse(words);
System.out.println(" The reversed string is ");
System.out.println(reversed);
}
}
Output:
The original string is
Hello World
The reversed string is
World Hello
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)