All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Reverse A String In Java Using For Loop

Last Updated : Jul 1, 2023

Reverse A String In Java Using For Loop

In this article we will show you the solution of reverse a string in java using for loop, as usual you need to import packages, in case you using eclipse then each program have itself. Here we taking a sample string then splitting into separate characters and stored on array variable.

To iterate array characters we need loop that’s why we defined for loop. Within loop we reversely concatenates each other then achieved the result.

Step By Step Guide On Reverse A String In Java Using For Loop :-

In class ‘Reverse’, we defined public static method, there we taken one sample string and using split() separating each characters in string variable ‘str’ and stored on array ‘strArr’ variable.

To reversely concatenates each separated characters we used for loop. Then we reach the target result at end of loop, lastly we printed reversed string on terminal. On the other hand we can do this simply with the help of string function later we will see about that.

//REVERSE STRING EXAMPLE
package java_prgms;
class Reverse
{
public static void main(String args[])
    {
        String str="Prawin";
        String[] strArr=str.split("");
        String t=" ";
        for(int i=0;i<strArr.length;i++){
         t=strArr[i]+t;
        }
        System.out.println(t);
    }
}
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. We defined class ‘Reverse’, there we defined public static method. Then ‘public static void main(String[] args)’ is defined that's starting point from where compiler starts program execution.
  3. You need to define string variable with one string, here we defined variable ‘str’ with value ‘Prawin’. Then we defined string array variable ‘strArr’ for stores string characters.
  4. Now you have to separate string characters, for that reason, we used split() method. The split method will separates character from selected string, using that we separated given string and stored on string array variable ‘strArr’.
  5. To concatenate purpose you need one empty string variable, so defined variable ‘t’. To iterate array characters we used for loop, it will process until reaches given string length.
  6. Within loop we inserting each characters reversely into string variable ‘t’, at last we printed result by println().
  7. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  8. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to reverse given string with for loop in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac Reverse.java’.

After successful compilation you need to execute ‘java Reverse‘ line for gets the output.

Then you can see output 'niwarP'. In case, you need to change given string value with any other, then the string and excute, you will get result as per changes. On eclipse just press of run button then you will get output inside terminal.

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