program to reverse a string using recursive algorithm in Java

August 22, 2014 , 0 Comments


Write a program to reverse a string using recursive methods.
You should not use any string reverse methods to do this.

package com.tutorialsdesk.algo;   
public class StringRecursiveReversal {       
String reverse = "";           
public String reverseString(String str){                   
if(str.length() == 1){             
return str;         
} else {             
reverse += str.charAt(str.length()-1)+reverseString(str.substring(0,str.length()-1));             
return reverse;         
}     
}           
public static void main(String a[]){         
StringRecursiveReversal srr = new StringRecursiveReversal();         
System.out.println("Result: "+srr.reverseString("Java2novice"));     
} 
}

program to reverse a string using recursive algorithm in Java

Hope we are able to explain you program to reverse a string using recursive algorithm in Java, if you have any questions or suggestions please write to us using contact us form.(Second Menu from top left).

Please share us on social media if you like the tutorial.

Prakash Hari Sharma
I'm a software developer and Java enthusiast. I like clean and modular code, enjoy Agile projects and have a passion for trying out new things. I try to learn and teach every day, and occasionally I'm even able to blog about it. :)