All TalkersCode Topics

Follow TalkersCode On Social Media

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

Java Program To Reverse Each Word In String

Last Updated : Mar 11, 2024

Java Program To Reverse Each Word In String

In this article we will show you the solution of java program to reverse each word in string, using the reverse(), split(), and substring() functions, we may reverse each word in a string.

Using the reverse () function of the String Builder class, we may reverse a supplied string.

The split("s") technique allows us to retrieve all of the words inside an array.

Use the substring() or charAt() methods to obtain the initial character.

Using the split() method is the simplest approach to separate a input string into an array of words.

By splitting a string around matches in a given regular expression, a string split() method can be used. After splitting, then it returns a String array.

Using the split() method, divide the string. After splitting a string, we have an array of words.

Take each word in an array one at a time. Reverse a word, then add it to the end of the string.

By employing a stack data structure, we may also reverse the words in a string. The last piece we put is the first we take out (LIFO). We can use a stack's property to fix this problem.

Utilize two stacks to implement the queue.

  • Declare a stack to hold the character type value.
  • Push every letter in a stack as you navigate a string until you reach a blank space. All the characters in a stack are popped when a space is encountered until the stack is empty. In a string, append the reverse words.

This strategy's time and spatial complexity is O (n).

The essential Java programming topics should be familiar to you in order to comprehend this programme:

  1. Java's for loop
  2. The split() function for Java
  3. String charAt() in Java

Step By Step Guide On Java Program To Reverse Each Word In String :-

public class TalkersCode
{
   public void TalkersCode(String str)
   {
 String[] words = str.split(" ");
 String reversedString = "";
 for (int i = 0; i < words.length; i++)
        {
           String word = words[i];
           String reverseWord = "";
           for (int j = word.length()-1; j >= 0; j--)
    {
  reverseWord = reverseWord + word.charAt(j);
    }
    reversedString = reversedString + reverseWord + " ";
 }
 System.out.println (str);
 System.out.println (reversedString);
   }
   public static void main(String[] args)
   {
 Example obj = new Example();
 obj.reverseWordInMyString("Welcome to BeginnersBook");
 obj.reverseWordInMyString("This is an easy Java Program");
   }
}
  1. First, we construct the TalkersCode class.
  2. The split() function of the String class is then defined.
  3. Following that, a string is divided into many strings based on the delimiter that was supplied as an argument.
  4. The charAt() function then creates the character that is located at the specified location in a string.
  5. The reverse string is then defined.

Conclusion :-

Using the split() method, divide the string. After splitting a string, we have an array of words.

Take each word in an array one at a time. Reverse a word, then add it to the end of the string.

By employing a stack data structure, we may also reverse the words in a string.

The last piece we put is the first we take out (LIFO). We can use a stack's property to fix this problem.

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

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪