All TalkersCode Topics

Follow TalkersCode On Social Media

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

String Reverse Function In Java

Last Updated : Mar 11, 2024

String Reverse Function In Java

In this article we will show you the solution of string reverse function in java, as usual first imported package, in case you using eclipse then each program have itself. Here you need to use ‘StringBuilder(), reverse() and toString()’ methods in java to achieve target result.

The StringBuilder() comprises of an inbuilt reverse() method, then you can used to reverse the character sequence in the reverse order.

To change the data type appending toString() method with that and result displayed.

Step By Step Guide On String Reverse Function In Java :-

The class ‘ReverseFun’ contains public static method, there you need sample string input to reverse.

So we defined string type variable ‘s’ with value. Then you need to pass the defined string variable into StringBuilder() function.

With that you need to bind reverse() and toString() methods then you will get the reverse order of given string. It is stored on string variable ‘res’ and printed at last through println().

//REVERSE STRING WITH STRING FUNCTION
package java_prgms;
class ReverseFun
{
 public static void main(String args[])
    {
        String s="Apple";
        String res=new StringBuilder(s).reverse().toString();
        System.out.println(res);
    }
}
  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. First you have to define class ‘ReverseFun’, which contains static main method. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. To show example of reverse string we need a string, here we defined string variable ‘s’ with value ‘Pawi’. Then you need to pass that into creation of StringBuilders() object, so we did it.
  4. Which is must appends with methods of ‘reverse(), toString()’, then only you will get string reverse value. To store the result we defined string variable ‘res’ and display result at terminal we used println() method.
  5. 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.
  6. 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 string function help in java.

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

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

Then you can see output 'iwaP'. In case, you need to change example string value then change it accordingly 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 string reverse function in java helps you and the steps and method mentioned above are easy to follow and implement.