mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
Merge pull request #22 from ashish4321/master
Reversing the string using recursion
This commit is contained in:
25
reverse string.java
Normal file
25
reverse string.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import java.io.*;
|
||||||
|
class stringreverse
|
||||||
|
{
|
||||||
|
String reverseString(String str)
|
||||||
|
{
|
||||||
|
String reverse=" ";
|
||||||
|
if(str.length()==1)
|
||||||
|
{
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reverse=reverse+str.charAt(str.length()-1)+reverseString(str.substring(0,str.length()-1));
|
||||||
|
return reverse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void main(String args[])
|
||||||
|
{
|
||||||
|
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
System.out.println("Enter the string");
|
||||||
|
String srr=br.readLine();
|
||||||
|
System.out.println("Reverse="+reverseString(srr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user