-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse.java
66 lines (51 loc) · 1.59 KB
/
Reverse.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
import java.io.*;
import java.util.*;
//1. reverse words in a string
//2. reverse vowels in a string
public class Reverse {
public String reverseVowels(String s) {
char[] str = s.toCharArray();
for(int i=0, j=s.length()-1;i<s.length()&& j>=0;i++,j--)
{
// if(s.charAt(i))
}
return s;
}
public static String reverseWord(String s)
{
String r = " ";
for(int i=s.length()-1;i>=0;i--)
{
r = r + s.substring(i, i+1);
}
return r;
}
public static List<String> reverseString(String s)
{
List<String> result = new ArrayList<String>();
String[] words = s.split(" ");
for(String w : words)
{
result.add(reverseWord(w));
}
return result;
}
public static String reverseString2(String s)
{
StringBuilder result = new StringBuilder();
String[] words = s.split(" ");
for(String w : words)
{
result.append(reverseWord(w));
}
return result.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "REST does not impose any restriction on the format of a resource representation. A client can ask for JSON representation where as another client may ask for XML representation of same resource to the server and so on. It is responsibility of the REST server to pass the client the resource in the format that client understands.";
List<String> reverselist = reverseString(str);
System.out.println(reverselist.toString());
String reversed = reverseString2(str);
System.out.println(reversed);
}
}