All TalkersCode Topics

Follow TalkersCode On Social Media

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

Reverse String Word Wise In Java

Last Updated : Mar 11, 2024

Reverse String Word Wise In Java

In this article we will show you the solution of reverse string word wise in java, in Java, a string is a series of characters that behaves like an object.

The sequence is indeed the second most popular and widespread used data structure after arrays.. It is a character array object that stores data.

Consider a string to be a character array in which you are able solve several more string-based problems. The java.lang package is required to create a string object.

The String class. A string in Java programming is represented by UTF -16. Strings are immutable, which means that there own internal state stays unchanged even after the object has been completely created.

The string object can perform a variety of operations, but the most commonly used function in Java is reverse strings.

We can use the methods reverse(), split(), and substring(). to reverse each word in a string.

The reverse() method of the StringBuilder class allows us to reverse a specified string.

Using the split("s") method, we can acquire all phrases in such an array. To obtain the first character, we can employ the substring() as well as charAt() methods.

Step By Step Guide On Reverse String Word Wise In Java :-

import java.util.regex.Pattern;
public class TalkersCodeExp {
 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 = "We love java";
     System.out.println(reverseWords(s2));
 }
}
  1. First, we make an import function called java.util.regex.pattern.
  2. The following step is to create a class named TalkersCodeExp.
  3. The following step is to generate a static string.
  4. The following step is to create a method for reversing the phrases in a String.
  5. Following the specification of a pattern to also be searched.
  6. After we create it, we iterate over temp array and store this same string in reverse.
  7. The if and else statements will be used to describe the series.
  8. As a result, as a string argument, a public static void main has been created.
  9. The following step is to generate a string value.
  10. Finally, we use system.out.println to exit the program.

Conclusion :-

We can use the methods reverse(), split(), and substring(). to reverse each word in a string.

The reverse() method of the StringBuilder class allows us to reverse a specified string.

Using the split("s") method, we can acquire all phrases in such an array.

To obtain the first character, we can employ the substring() as well as charAt() methods.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪