1. Introduction
In this article, You’re going to learn how to reverse a string using recursion approach. The first program is to reverse a string and the second program will read the input from the user.
2. What Is Recursion
Recursion means in computer science is that a method calling the same function with different input.
The recursive method must have at least one argument.
This approach solves many complex programs easily but you have to be very careful otherwise will create StackOverflow or outofmemoryerror.
3. Example Program to Reverse String using Recursion
To understand this program you should know two String class methods and those are charAt() and substring() methods.
package com.javaprogramto.w3schools.programs.string;
public class StringReverseRecursion {
public static void main(String[] args) {
String s1 = "Welcome to the javaprogramto.com";
String reversedS1 = reverseString(s1);
System.out.println("String s1 before reversing : "+s1);
System.out.println("Reversed String s1 : "+reversedS1);
String s2 = "Another String s2";
String reversedS2 = reverseString(s2);
System.out.println("String s2 before reversing : "+s2);
System.out.println("Reversed String s2 : "+reversedS2);
}
private static String reverseString(String sentense)
{
if (sentense.isEmpty())
return sentense;
//Calling method Recursively
return reverseString(sentense.substring(1)) + sentense.charAt(0);
}
}
Output:
String s1 before reversing : Welcome to the javaprogramto.com
Reversed String s1 : moc.otmargorpavaj eht ot emocleW
String s2 before reversing : Another String s2
Reversed String s2 : 2s gnirtS rehtonA
4. Another Example to reverse String reading from the user
In this program, the User has to enter the string to be reversed. Scanner class nextLine() method is used to read the input string from the user keyboard and pass the string value to the recursive method reverseString().
package com.javaprogramto.w3schools.programs.string;
import java.util.Scanner;
public class StringReverseRecursionFromUser {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter String One");
String s1 = scanner.nextLine();
String reversedS1 = reverseString(s1);
System.out.println("String s1 before reversing : "+s1);
System.out.println("Reversed String s1 : "+reversedS1);
System.out.println("Enter String Two");
String s2 = scanner.nextLine();
String reversedS2 = reverseString(s2);
System.out.println("String s2 before reversing : "+s2);
System.out.println("Reversed String s2 : "+reversedS2);
}
private static String reverseString(String sentense)
{
if (sentense.isEmpty())
return sentense;
//Calling method Recursively
return reverseString(sentense.substring(1)) + sentense.charAt(0);
}
}
Output:
Enter String One
Reading from user
String s1 before reversing : Reading from user
Reversed String s1 : resu morf gnidaeR
Enter String Two
String entered by user
String s2 before reversing : String entered by user
Reversed String s2 : resu yb deretne gnirtS
0 comments:
Post a Comment