-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseWords.java
More file actions
38 lines (31 loc) · 823 Bytes
/
ReverseWords.java
File metadata and controls
38 lines (31 loc) · 823 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
28
29
30
31
32
33
34
35
36
37
38
public class ReverseWords {
public static String reverse(String str){
int i = str.length()-1;
String s = "";
while(i>=0){
// for spaces
while(i>=0 && str.charAt(i) == ' '){
i--;
}
int j = i;
if(i < 0){
break;
}
while(i>=0 && str.charAt(i) != ' '){
i--;
}
if(s.isEmpty()){
s = s.concat(str.substring(i+1, j+1));
}
else{
s = s.concat(" "+ str.substring(i+1, j+1));
}
}
// str = (String)sb;
return s;
}
public static void main(String args[]){
String s = "I am bhupendra maurya";
System.out.println(reverse(s));
}
}