All TalkersCode Topics

Follow TalkersCode On Social Media

How To Reverse A String In Java Word By Word

Last Updated : Jan 1, 2023

How To Reverse A String In Java Word By Word

In this article we will show you the solution of how to reverse a string in java word by word, in Java you can reverse a String in a variety of ways, when a string is reversed, the character in the original string's ith position will now be in position (string length -i) the new reversed string's nth position.

We'll walk through a few samples Java programmes that show how to reverse a string in this lesson.

Step By Step Guide On How To Reverse A String In Java Word By Word :-

In Java, a string is a cluster of characters that is regarded as an object. The String object has a wide range of features available on it in Java.

String Reverse is one of the most common used operations on a string object. Here we will explain the various methods for reversing a string in Java in this segment.

A String can be flipped five distinct ways in Java. These are what they are:

  • Using the CharAt Method, reverse a String.
  • Using a String Buffer/String Builder Approach to Reverse a String
  • Recursion is used to reverse a string in the reverse iterative approach.
  • Reverse the String's letter sequences.

Using the CharAt Method, reverse a String.

The Java application that is provided below will assist you in understanding how to reverse a String that the user has typed.

To extract the characters from the input String in this case, I used the CharAt() method. The main function of the CharAt() method is to return the character that is located at the particular String's specified index.

It is then added them to the end of the provided String in aboslute reverse order. One of the straightforward methods in Java for reversing a String is this particular method.

Use the String Builder or String Buffer Class to reverse a string.

The function of Reversing the characters in a StringBuffer is done using the built-in method reverse() of the StringBuffer and StringBuilder classes.

With the help of this technique, the original character sequence is restored. The program to reverse a String using a StringBuffer class built-in method is there below.

Using Reverse Iteration to Reverse a String

In this method, I first used the CharArray() method to transform the supplied String to a Character Array.

Recursive String Reversal

A function that calls itself is what recursion is. In this function, I'll create a method that calls itself repeatedly while reversing the String. So, Let's try it out and see how it goes.

Reverse the String's letter sequences.

This Java software reverses the letters in a String that the user has entered.

As was the case with the other methods, it does not reverse the entire String. As an illustration, "Hello People" will be written as "olleH elpoeP." Let's use Java to implement the same.

import java.util.*;
public class ReverseStringDemo
public static void main(String[] arg)
ReverseStringDemo rs=new ReverseStringDemo();
Scanner sc=new Scanner(;
System.out.print("Enter a string: ");
String str=sc.nextLine();
System.out.println("Reverse of a String is : "+rs.reversestr(str)); //called method
//reverse string method
static String reversestr(String s)
String r="";
for(int i=s.length();i>0;--i) //execute until condition i>0 becomes false
return r;
  1. Importing package java.util.*;
  2. Creating a public class ReverseStringDemo
  3. Creating object sc of Scanner class
  4. Print a statement “Enter a String”.
  5. String scr=sc.nextLine(): to print on next line
  6. Printing a statement reverse of a string that will be stored in rs.reversestr
  7. Then, we will execute the for loop until the condition totally becomes false I.e., i>0 becomes untrue
  8. r=r+(s.charAt(i-1)); that will return r
  9. Lastly, run the code

Conclusion :-

So, in this segment of we learned about reversing string in java word by word with the help of for loop. In the next tutorial we will learn more about Java string.

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

Latest Tutorials