-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringManipulation.java
69 lines (68 loc) · 1.94 KB
/
StringManipulation.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package codingProblems;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
public class StringManipulation {
public static void main(String args[])
{
StringManipulation s = new StringManipulation();
Scanner in = new Scanner(System.in);
System.out.println("Enter a string:");
String str = in.nextLine();
s.reverseString1(str);
s.reverseString2(str);
System.out.println("Reversed string by method 3:\n"+s.recursiveReverseString(str));
String[] strArray = str.split(" ");
System.out.println("Reversed string by method 4:");
for(String st:strArray)
System.out.print(s.reverseRecursiveDivide(st)+" ");
in.close();
}
public void reverseString1(String str)
{
char[] strArr = str.toCharArray();
String reverseString = "";
for(int i=strArr.length-1;i>=0;i--)
{
reverseString = reverseString+strArr[i];
}
System.out.println("Reversed string by method 1:\n"+reverseString);
}
public void reverseString2(String str)
{
char[] strArr=str.toCharArray();
List<Character> trial1= new LinkedList<>();
for(char c: strArr)
trial1.add(c);
Collections.reverse(trial1);
ListIterator<Character> li = trial1.listIterator();
System.out.println("Reversed string by method 2:");
while(li.hasNext())
System.out.print(li.next());
System.out.println();
}
String reverse="";
public String recursiveReverseString(String str)
{
if(str.length() == 1)
{
return str;
}
else
{
reverse += str.charAt(str.length()-1)
+recursiveReverseString(str.substring(0,str.length()-1));
return reverse;
}
}
public String reverseRecursiveDivide(String str)
{
if (str.length() <= 1) {
return str;
}
return reverseRecursiveDivide(str.substring(str.length() / 2, str.length())) +
reverseRecursiveDivide(str.substring(0, str.length() / 2));
}
}