All TalkersCode Topics

Follow TalkersCode On Social Media

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

Reverse A String Using Recursion In Java

Last Updated : Mar 11, 2024

Reverse A String Using Recursion In Java

In this article we will show you the solution of reverse a string using recursion in java, first we imported package and if you are using eclipse then all program will import itself during creation. In main class we defined main static method and rev() methods.

The rev() method processing passed string with StringBuilders to appends the reverse method and reverse the given string return to the main method. Returned result stored on variable ‘Result’ and displayed on terminal by println() method.

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

In main class ‘Revers_prgm’, we defined main and rev() method definitions. In rev() function, using if condition we checks whether passed argument has value or null.

In case its null we sends as it is to the main method otherwise, we passing parameter of string into StringBuilder() method.

Now you can appends that with reverse() method and using toString() method we can get string type result then printed on terminal.

//REVERSE STRING
package java_prgms;
class Revers_prgm
{
 public static String rev(String s){
  if(s==null)
   return s;
  else
   return new StringBuilder(s).reverse().toString();
 }
 public static void main(String args[])
    {
        String s1="Language";
        String Result=rev(s1);
        System.out.println(Result);
    }
}
  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. Here we defined class ‘Revers_prgm’, which contains static main and rev() methods. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. There we defining variable ‘s1’ with initialized value ‘Language’ and calling method rev with argument ‘s1’ and retuned result stored on string variable ‘Result’ and printed on terminal.
  4. In method rev() we checking passed variable value whether null or not. If true then returning that value to main method otherwise, creation of StringBuilder() method we passing the argument ‘s’.
  5. Then you can reverse that string ‘s’ by binding that with reverse() and toString() method used to convert results data type as string and returned to main method.
  6. In main method returned result printed on terminal successfully. Here we achieved the result with the help of StringBuilder method.
  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 string by recursive method in java.

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

After successful compilation you need to execute ‘java Revers‘ line for gets the output. Then you can see output 'egaugnaL'.

On eclipse just press of run button then you will get output inside terminal.

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