Java
Java

 

Introduction

Reversing a string is a common programming task that involves creating a new string that is the reverse of the original. This exercise helps in understanding string manipulation and basic algorithms.
In this example, we will implement a Java program that takes an input string and returns its reverse.

Objective

The objective of this program is to demonstrate how to reverse a given string using Java.
By the end of this example, you will be able to understand the basic structure of a Java program, string handling, and how to manipulate character arrays.

Java Code to Reverse a String


import java.util.Scanner;

public class ReverseString {
    public static void main(String[] args) {
        // Create a Scanner object to read input
        Scanner scanner = new Scanner(System.in);
        
        // Prompt user for input
        System.out.print("Enter a string to reverse: ");
        String input = scanner.nextLine();
        
        // Reverse the string
        String reversed = reverse(input);
        
        // Display the reversed string
        System.out.println("Reversed String: " + reversed);
        
        // Close the scanner
        scanner.close();
    }

    // Method to reverse the string
    public static String reverse(String str) {
        StringBuilder reversedString = new StringBuilder();
        for (int i = str.length() - 1; i >= 0; i--) {
            reversedString.append(str.charAt(i));
        }
        return reversedString.toString();
    }
}
        

Explanation of the Program Structure

The program consists of a single class named ReverseString containing the main method and a helper method called reverse.

  • Import Statement: We import the Scanner class to allow user input from the console.
  • Main Method: This is the entry point of the program. Here, we create a Scanner object to read user input, prompt the user for a string, and call the reverse method to get the reversed string.
  • Reverse Method: This method takes a string as input and uses a StringBuilder to build the reversed string. We iterate through the original string backwards, appending each character to the StringBuilder.
  • Output: Finally, we print the reversed string to the console.

How to Run the Program

To run this Java program, follow these steps:

  1. Ensure you have Java installed on your machine. You can download it from here.
  2. Create a new file named ReverseString.java and copy the provided code into it.
  3. Open your command line or terminal.
  4. Navigate to the directory where the file is saved.
  5. Compile the program using the command: javac ReverseString.java.
  6. Run the compiled program with the command: java ReverseString.
  7. Follow the on-screen prompts to enter a string and see the reversed output.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)