-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseWordsinaString.java
49 lines (38 loc) · 1001 Bytes
/
ReverseWordsinaString.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
package ProjectFiles;
public class ReverseWordsinaString {
public static void main(String[] args) {
String s = " risk ffsd or life ";
System.out.println(reverseWords(s));
//System.out.println(s);
}
public static String reverseWords(String s) {
s = s.trim();
char[] str = s.toCharArray();
reverse(str,0,str.length -1);
int cur = 0, i = 0, start = 0, end =0;
while( i < str.length){
while( i < str.length && str[i] == ' ') i++;
start = i;
while(i < str.length && str[i] != ' ') i++;
end = i-1;
reverse(str,start, end);
for(int a = cur, b = start; b <= end; a++, b++)
str[a] = str[b];
cur = cur + end - start +2;
if(cur -1 <str.length )
str[cur - 1] = ' ';
}
while(cur < s.length()){
str[cur++] = ' ';
}
String result = new String(str);
return result.trim();
}
private static void reverse(char[] s, int a, int b) {
for(int i = a, j = b; i < j ; i++, j--){
char c = s[i];
s[i] = s[j];
s[j] = c;
}
}
}