We use cookies to improve your experience. If you continue to use our site, we'll assume that you're happy with it. :)

Reverse a String of User Input in Java Programming

String is a sequence of characters that is considered to be an object in Java. In Java, there are various operations that you can perform on the String object. One of the most widely used operations on a string object is String Reverse.

Java program to reverse a string that a user inputs. In this Program, the User Input will be totally reversed as typed. For Example: See the Source Code and Output.

Source Code:

import java.util.*;
class ReverseString
{

  public static void main(String args[])

  {

    String original, reverse = "";
    Scanner in = new Scanner(System.in);
    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 the string: " + reverse);
  }
}

Post a Comment