Java Program To Reverse A String

Chapter: Miscellaneous Last Updated: 22-04-2023 03:59:37 UTC

Program:

            /* ............... START ............... */
                
import java.util.Scanner;

public class ReverseString {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string to reverse: ");
        String original = scanner.nextLine();
        String reversed = reverseString(original);
        System.out.println("Reversed string: " + reversed);
    }

    public static String reverseString(String str) {
        String reversed = "";
        for (int i = str.length() - 1; i >= 0; i--) {
            reversed += str.charAt(i);
        }
        return reversed;
    }
}

                /* ............... END ............... */
        

Output

Enter a string to reverse: Hello, world!
Reversed string: !dlrow ,olleH

Enter a string to reverse: Java is fun
Reversed string: nuf si avaJ

Enter a string to reverse: OpenAI is awesome
Reversed string: emosewa si IAnePO

Notes:

  • The program uses the Scanner class to prompt the user to enter a string to reverse. Once the user enters the string, the program calls the reverseString method to reverse the string.
  • The reverseString method takes in the original string and iterates over its characters from the last character to the first character using a for loop. During each iteration, the method appends the current character to a new string variable called reversed. By iterating over the characters in reverse order, the reverseString method effectively creates a reversed version of the original string.
  • Once the reverseString method has reversed the original string, it returns the reversed string to the main method. The main method then prints out the reversed string to the console.
  • Overall, this program is a simple and straightforward implementation of the algorithm to reverse a string in Java.

Tags

java program to reverse a string #Reverse a string in Java using for loop #How to reverse a string in java

Similar Programs Chapter Last Updated
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023
Java Program To Run Something Every X Minutes Miscellaneous 22-04-2023
Java Program To Implement Method Overloading Miscellaneous 16-04-2023
Java Parse JSON String To Object Miscellaneous 24-03-2023
Java Program For Date And Time Miscellaneous 24-03-2023
Java Binary Search Tree Implementation Miscellaneous 21-03-2023
Java Program To Merge Two PDF Files Miscellaneous 18-03-2023
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Currency Formatter In Java | How To Format Currency In Java Miscellaneous 19-07-2021
Factory Design Pattern In Java Miscellaneous 11-05-2021
Data Types In Java Miscellaneous 09-06-2018

1