Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

Simply Reverse A String in Java Using Different Methods

Using While Loop or For Loop

Simply handle the string within the while loop or the for loop. Get the length of the string with the help of a cursor move or iterate through the index of the string and terminate the loop.

The loop prints the character of the string where the index (i-1).

The loop starts and iterates the length of the string and reaches index 0.

The term “string” refers to a set of characters that behaves as objects in Java. Strings are among the most commonly used and utilized data structures, following arrays. This is an object which stores data in a string array.

To make it clearer, take a look at a string as character array that allows you to solve a variety of string-related problems.

For the creation of a String object using the java.lang.String class is needed. Java Program makes use of UTF -16 to represent a string. Strings are immutable, which means that their internal state stays the same once the object has been constructed. String objects perform a variety of actions, but the most frequently used is reverse.

Reverse in Java

Example: JAVA string reverse and provide the output as the AVAJ

Different Ways to Reverse a String

Since strings are immutable objects you must create a new string that reverses them. The string class doesn’t include reverse methods that reverse the strings. It uses the toCharArray() technique that can reverse the process.

Through the use of toCharArray()

The following code will assist you in understanding how reverse the sequence of a string. Through the use of toCharArray() technique is the most common method that reverses a string using Java.

The code also makes use of length, which provides the length total of the variable string.

The for loop repeats until it reaches the point at which index zero is reached.


//Reverse String using Charcter Array.

public static void main(String[] arg) {

//declaring of variable

String stringinput = “GOODMORNING”;

        //convert String to character array

        //by using toCharArray

        char[] resultarray = stringinput.toCharArray();


        for (int i = resultarray.length – 1; i >= 0; i–)

         //print reversed String





Using While Loop

Simply manipulate the string inside the while loop or for loop. Find an estimate of length using the aid of a cursor movement or traverse the index to close the loop.

This loop print the string’s character in which the index is (i-1).

The loop starts , iterating over the entire length until it is at the index of 0.

Using While Loop


// Java program to reverse a string using While loop

import java.lang.*;


import java.util.*;

public class strReverse {

    public static void main(String[] args)


    String stringInput = “You Are A Good Boy”;  

    //Get the String length

    int iStrLength=stringInput.length();

    //Using While loop

while(iStrLength >0)


System.out.print(stringInput.charAt(iStrLength -1));






yoB dooG A erA uoY      

Reverse a String in Java Word by Word

It is possible to reverse an unicode string using Java (word BY word) with the built-in Java String Split() procedure. All you have to do is pass whitespace when using the Split() method.

Look sample program below.

import java.util.Scanner;

public class Reverse {

    public static void main(String[] args) {

        String str;

        /* Getting input from console using Scanner class



        Scanner in = new Scanner(;

        System.out.println(“Enter your String”);

        str = in.nextLine();


         *  Used split() method to print in reverse order

         *  delimited by whitespace


        String[] split = str.split(” “);   

        for(int i=split.length-1; i>=0; i–)


            System.out.print(split[i] + ” “);





Enter Your String

Prince Rahul

Rahul  Prince

Categorized as Java

Leave a comment

Your email address will not be published. Required fields are marked *