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"));
}
}
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.
Source:http://www.tutorialsdesk.com/2014/08/program-to-reverse-string-using.html
Tidak ada komentar:
Posting Komentar