Java Program to Reverse a String

Program

import java.util.Scanner;
public class ReverseString
{
    public static void main(String[] args)
    {
        System.out.println("Enter string to reverse:");
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String reverse = "";
        for(int i = str.length() - 1; i >= 0; i--)
        {
            reverse += str.charAt(i);
        }
        System.out.println("Reversed string is: " + reverse);
    }
}

Let us understand the program by breaking into simple parts: The class ReverseString demonstrates the logic to reverse a given string. The program first displays a message asking the user to enter a string to reverse: System.out.println("Enter string to reverse:");. The user is prompted to Enter a number using System.out.println("Enter string to reverse:"); and the user input is read using Scanner object using Scanner sc = new Scanner(System.in); and inputted value is stored in str variable.

The main logic to reverse the String:

  1. Initialize the reverse String variable.
  2. The program utilizes a for loop to iterate through the characters of the input string in reverse order.
  3. The loop starts from the index corresponding to the last character of the input string (str.length() - 1) and iterates until the first character (i=0) is encountered.
  4. Inside the for loop, each character of the input string is appended to the reverse string variable. reverse += str.charAt(i); concatenates the characters from the input string str starting from the last character and moving towards the first character. This effectively reverses the order of characters in the input string.
  5. Once the loop completes, the reversed string stored in the reverse variable is printed to the console using System.out.println("Reversed string is: " + reverse);.

Consider an example to illustrate step by step process of reversing a number:

Let's say the user enters the String scoop.

  1. In the first iteration of the loop:
    • The character at index 4 (last character) of the input string "scoop" is 'p'.
    • 'p' is appended to the reverse string.
    • reverse = "p".
  2. In the second iteration of the loop:
    • The character at index 3 of the input string "scoop" is 'o'.
    • 'o' is appended to the reverse string.
    • reverse = "po".
  3. In the third iteration of the loop:
    • The character at index 2 of the input string "scoop" is 'o'.
    • 'o' is appended to the reverse string.
    • reverse = "poo".
  4. In the fourth iteration of the loop:
    • The character at index 2 of the input string "scoop" is 'c'.
    • 'c' is appended to the reverse string.
    • reverse = "pooc".
  5. In the fifth iteration of the loop:
    • The character at index 2 of the input string "scoop" is 's'.
    • 's' is appended to the reverse string.
    • reverse = "poocs".

This process continues until all characters of the input string are appended to the reverse string in reverse order. After the loop completes, the reversed string "poocs" stored in the reverse variable is printed to the console.

Output

Enter string to reverse:
welcome to oodlescoop
Reversed string is: poocseldoo ot emoclew

Tags: