forked from sat5297/hacktober-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseLine.java
More file actions
47 lines (35 loc) · 782 Bytes
/
ReverseLine.java
File metadata and controls
47 lines (35 loc) · 782 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
39
40
41
42
43
44
45
46
47
import java.util.Scanner;
public class QuickSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String ans = "";
int spaces = 0, len = str.length()-1;
while(len>=0) {
while(len >= 0 && str.charAt(len) == ' ') {
len--;
spaces++;
}
int j = len;
if(len<0) { break;}
while(len >= 0 && str.charAt(len) != ' ') {
len--;
}
if(ans.isEmpty()) {
ans = ans.concat(str.substring(len+1,j+1));
}else {
while(spaces >= 0) {
if(spaces == 0) {
ans = ans.concat(str.substring(len+1,j+1));
break;
}
ans = ans.concat(" ");
spaces--;
}
spaces = 0;
}
}
str = ans.toString();
System.out.println(str);
}
}