Skip to content

Commit 382809c

Browse files
authored
Merge pull request ephremdeme#175 from ishaangupta-YB/patch-1
Create ReverseLineWordByWord
2 parents 4c23f19 + e606d1b commit 382809c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

problems/ReverseLineWordByWord

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.Scanner;
2+
3+
public class QuickSort {
4+
5+
public static void main(String[] args) {
6+
7+
Scanner sc = new Scanner(System.in);
8+
9+
String str = sc.nextLine();
10+
String ans = "";
11+
12+
int spaces = 0, len = str.length()-1;
13+
14+
while(len>=0) {
15+
while(len >= 0 && str.charAt(len) == ' ') {
16+
len--;
17+
spaces++;
18+
}
19+
int j = len;
20+
21+
if(len<0) { break;}
22+
23+
while(len >= 0 && str.charAt(len) != ' ') {
24+
len--;
25+
}
26+
27+
if(ans.isEmpty()) {
28+
ans = ans.concat(str.substring(len+1,j+1));
29+
30+
}else {
31+
while(spaces >= 0) {
32+
if(spaces == 0) {
33+
ans = ans.concat(str.substring(len+1,j+1));
34+
break;
35+
}
36+
ans = ans.concat(" ");
37+
spaces--;
38+
}
39+
spaces = 0;
40+
}
41+
}
42+
43+
str = ans.toString();
44+
System.out.println(str);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)