Java Program to Reverse a String

In the below JAVA programming tutorial, we will make a Java program to reverse any string entered by the user. For example, if user has entered "xyz" in the program, then it will produce an output of "zyx" after reversing the string. In this Java program, we will use charAt() function to reverse the given string.

Java Program Code to Reverse any String

import java.util.*;

class ReverseString
   public static void main(String args[])
      String original, reverse = "";
      Scanner in = new Scanner(;

      System.out.println("Enter a string to reverse");
      original = in.nextLine();

      int length = original.length();

      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);

      System.out.println("Reverse of entered string is: "+reverse);

Output of Java Program for reversing any given String


Post a Comment