program to reverse a string using recursive algorithm in Java


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.
SHARE
    Blogger Comment
    Facebook Comment