-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStringReverseAnsSort.java
More file actions
67 lines (62 loc) · 2.1 KB
/
StringReverseAnsSort.java
File metadata and controls
67 lines (62 loc) · 2.1 KB
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
67
import java.util.Scanner;
import java.util.StringTokenizer;
public class StringReverseAnsSort {
// public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// System.out.println("Enter the number of line: ");
// int n = sc.nextInt();
// if (n > 20) {
// System.out.println("Number of line limit is 20");
// System.exit(0);
// }
// String line[] = new String[n];
// String s = "";
// for (int i = 0; i < line.length; i++) {
// line[i] = sc.nextLine();
// if (line[i].length() > 80) {
// System.out.println("Length of sentence should not be greater than 80");
// i--;
// }
// }
// for (int i = n-1; i >= 0; i--) {
// StringTokenizer stz = new StringTokenizer(line[i],".,;:'");
// while (stz.hasMoreTokens()) {
// s=" "+stz.nextToken()+s;
// }
// }
// System.out.println(s);
// }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String st = sc.nextLine();
String[] word = st.split(" ");
for (int j = 0; j < word.length - 1; j++) {
for (int j2 = 0; j2 < word.length - j - 1; j2++) {
if (word[j2].length() > word[j2 + 1].length()) {
String rev = word[j2 + 1];
word[j2 + 1] = word[j2];
word[j2] = rev;
}
}
}
String s2 = "";
int flag = 0;
for (int j = 0; j < word.length; j++) {
String ocr = "";
flag = 0;
for (int j2 = j + 1; j2 < word.length - 1; j2++) {
if (word[j].length() == word[j2].length()) {
flag = 1;
if (st.indexOf(word[j]) < st.indexOf(word[j2]))
ocr = word[j];
}
}
if (flag == 0)
s2 += word[j] + " ";
else
s2 += ocr + " ";
}
System.out.println("Result: " + s2);
}
}