All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Reverse The Words In A String Java

Last Updated : Mar 11, 2024

Reverse The Words In A String Java

In this article we will show you the solution of reverse the words in a string java, in this problem "Reverse words in a String", An input string, s, is provided to us.

To return the string of a words in reverse order, we must switch the order of a words.

Approach 1 - Reversing the process of string splitting & saving

Splitting the string & saving this in reverse order is among the simplest techniques to reverse words inside a string.

This is the naive approach.

Complexity Analysis

Let's analyze the time and space complexity of this approach :

  • Time Complexity : O(n) as we use only one for a loop.
  • Space Complexity : O(n) as we are storing our output that is, the reversed string in a new variable.

Approach 2 - Using the Two-Pointers Approach

Now, here comes the optimal solution for this problem.

At the start and the end of the string, respectively, two pointers are initialised.The string is then inverted by switching the words at the beginning and finish.

Algorithm At the start and the end of the string, respectively, two pointers are initialised. The string is then inverted by switching the words at the beginning and finish.

  1. Convert the word-containing string s into such an array with string expressions.
  2. Set both right and left and pointers to 0 and s.length() - 1 respectively.
  3. These elements at the right and left points must now be switched. Move your left pointer there for that.While the left pointer doesn't really exceed the right pointer, move the right pointer one position forward and right pointer backward, swapping the items at each location.
  4. Return the final string.

Complexity Analysis

Let's analyze the time and space complexity of this approach:

  • Time Complexity : O(n)
  • Space Complexity : O(1)

Step By Step Guide On Reverse The Words In A String Java :-

import java.util.regex.Pattern;
public class TalkersCode {
 static String reverseWords(String str)
 {
  Pattern pattern = Pattern.compile("\\s");
  String[] temp = pattern.split(str);
  String result = "";
  for (int i = 0; i < temp.length; i++) {
   if (i == temp.length - 1)
    result = temp[i] + result;
   else
    result = " " + temp[i] + result;
  }
  return result;
 }
 public static void main(String[] args)
 {
  String s1 = "Welcome to TalkersCode";
  System.out.println(reverseWords(s1));
  String s2 = "TalkersCode Java";
  System.out.println(reverseWords(s2));
 }
}
  1. First we create import function as java.util.regex.pattern.
  2. Then we create class as TalkersCode.
  3. After that we Specifying the pattern to be searched
  4. Then we splitting String str with a pattern
  5. Then we Iterate over the temp array and store the string in reverse order.
  6. After that we create a string function.
  7. After successful creating we close program.

Conclusion :-

An extremely significant issue with regard to interview preparation is reverse words inside a string.

Splitting the string & storing it in reverse order is among the simplest solutions to this issue.

Another method to create the reversed string is to switch the words at the beginning and finish. Additionally, this is the best answer to the issue.

I hope this article on reverse the words in a string java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪