-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseString.java
More file actions
27 lines (23 loc) · 655 Bytes
/
ReverseString.java
File metadata and controls
27 lines (23 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class ReverseString
{
public static String reverse(String str){
if(str.length() == 0){
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
public static String UsingStringBuffer(String str){
StringBuilder sb = new StringBuilder();
for(int i = str.length() - 1 ; i >= 0; i--){
sb.append(str.charAt(i));
}
return sb.toString();
}
public static void main(String[] args)
{
String str = "java";
String result = reverse(str);
System.out.println("Reverse of a string is :: " + result);
System.out.println("Reverse using StringBuilder" + UsingStringBuffer(str));
}
}