w3resource

Java Project - Program to Reverse a String

Reverse a String using a For Loop and Recursion:

Reverse a String:

Input: A string.
Output: The reversed string.

Example:

  • Input: "hello"
  • Output: "olleh"
  • Input: "Java"
  • Output: "avaJ"

Solution 1: Reverse a String using a For Loop

Code:

import java.util.Scanner;

public class ReverseString {
    // Method to reverse the string using a for loop
    public static String reverseUsingLoop(String input) {
        String reversed = "";  // Initialize an empty string to store the reversed result

        // Loop through the string from the last character to the first
        for (int i = input.length() - 1; i >= 0; i--) {
            reversed += input.charAt(i);  // Append each character to the reversed string
        }

        return reversed;  // Return the reversed string
    }

    // Main method to take user input and display the reversed string
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Taking user input
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();

        // Reversing and displaying the result
        String reversed = reverseUsingLoop(input);
        System.out.println("Reversed string: " + reversed);
        
        scanner.close();  // Close the scanner to prevent resource leakage
    }
}   

Output:

Enter a string:  Java Exercises
Reversed string: sesicrexE avaJ
Enter a string:  Project
Reversed string: tcejorP

Explanation :

  • Input: User inputs a string.
  • Processing: The program iterates through the string from the last character to the first, appending each character to form a new string.
  • Output: The program displays the reversed string.

Solution 2: Reverse a String using Recursion

Code:

import java.util.Scanner;

public class RecursiveReverseString {
    // Recursive method to reverse the string
    public static String reverseUsingRecursion(String input) {
        // Base case: If the string is empty, return an empty string
        if (input.isEmpty()) {
            return input;
        }

        // Recursive case: Return the last character + result of the rest of the string reversed
        return reverseUsingRecursion(input.substring(1)) + input.charAt(0);
    }

    // Main method to take user input and display the reversed string
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Taking user input
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();

        // Reversing and displaying the result
        String reversed = reverseUsingRecursion(input);
        System.out.println("Reversed string: " + reversed);
        
        scanner.close();  // Close the scanner
    }
}

Output:

Enter a string:  RecursiveReverseString
Reversed string: gnirtSesreveRevisruceR
Enter a string:  madam
Reversed string: madam

Explanation:

  • Input: User enters a string.
  • Processing: The recursive function processes the string by taking the last character and calling the function again on the rest of the string, building the reversed string step by step.
  • Output: The program prints the reversed string.

Java Code Editor:




Follow us on Facebook and Twitter for latest update.