11 March, 2018

Write a program to reverse a string using recursive method


The below example is to reverse string in java using recursive method.



package com.javadevelopersguide.www;
/**
 *
 * This Program will help you to reverse the string using recursive manner.
 *
 * @author manojkumar.bardhan
 *
 */
public class ReverseStringRecursive {
String resultString = "";
public static void main(String[] args) {
ReverseStringRecursive rec = new ReverseStringRecursive();
String inputString = "javadevelopersguide";
System.out.println("Input String::" + inputString);
String stringRev = rec.callReverse(inputString);
System.out.println("New Result String::" + stringRev);
}
private String callReverse(final String str) {
if (str.length() == 1) {
return str;
} else {
resultString += str.charAt(str.length() - 1)
+ callReverse(str.substring(0, str.length() - 1));
return resultString;
}
}
}

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Really useful information. Thank you so much for sharing.It will help everyone.
    AWS Online Training
    AWS Training in Hyderabad
    Amazon Web Services Online Training

    ReplyDelete
  3. Very nice information on java. You can also check goformule.com for mulesoft tutorials

    ReplyDelete
  4. wesome article! You are providing us very valid information. This is worth reading. Keep sharing more such articles. All JAVA Tutorials In one place, Thank you very much!❤❤❤

    ReplyDelete
  5. Thanks for sharing.. You are providing us very valid information.


    Java Developer Jobs available in Chennai

    ReplyDelete