All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Java Program To Reverse A String

Last Updated : Jul 1, 2023

Java Program To Reverse A String

In this article we will show you the solution of java program to reverse a string, Java is a well-liked object-oriented programming language that is extensively used to build websites and apps.

A string is a collection of characters; to reverse a string is to flip its characters so that the final character becomes the first and vice versa.

In many programming contexts, including sorting, searching, and encryption, reversing a string is a crucial process.

Java provides a variety of methods for reversing strings, ranging from using an inbuilt function to developing a unique technique.

We will now consider the idea of a Java programming that reverses a string.

Step By Step Guide On Java Program To Reverse A String :-

public class Main {
  public static void main(String[] args) {
    String originalStr = "Hello World";
    String reversedStr = "";
    System.out.println("Original string: " + originalStr);
    for (int i = 0; i < originalStr.length(); i++) {
      reversedStr = originalStr.charAt(i) + reversedStr;
    System.out.println("Reversed string: "+ reversedStr);
  1. Here, you can see how we created the Java code for a programme that uses a loop approach to reverse a string.
  2. We initialise the string variables "originalStr" and "reversedStr" at the beginning of the code.
  3. The string that will be inverted, "originalStr", has the value "Hello World".
  4. The initialization of the "reversedStr" is a null string.
  5. The code uses the System.out.println() function to print the original string to the console.
  6. Then, using a for loop, a loop is used, iterating from 0 to the length of the original string.
  7. Utilizing the charAt() method, which returns a character at a defined index within a string, the code extracts the character at the present index for each iteration.
  8. The "+" operator is then used to combine the extracted character with the currently reversed string.
  9. Once every character has been extracted and added to the reversed string, the loop repeats this process until it has gone through the entire string.
  10. The original string is reversed in "reversedStr"'s final value.
  11. The final line of code uses the System.out.println() function to print the inverted string to the console.

Conclusion :-

As a result, we have successfully learned how to reverse a string using a Java programme.

Additionally, we discovered that reversing a string with a loop in Java is a quick and effective process.

Two string variables are initialised by the programme, one for the original string and the other for the reversed string.

The loop goes through the original string character by character, removes each character, and then concatenates it to the start of the inverted string.

The inverted string's final value is printed to the console.

This programme displays the flexibility of the language in constructing multiple algorithms for various programming contexts while also demonstrating the essential ideas of string manipulation in Java.

I hope this article on java program to reverse a string helps you and the steps and method mentioned above are easy to follow and implement.