-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentenceReverse.java
More file actions
43 lines (41 loc) · 1.43 KB
/
Copy pathSentenceReverse.java
File metadata and controls
43 lines (41 loc) · 1.43 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
package basic;
import java.io.*;
import java.util.StringTokenizer;
class SentenceReverse {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no of sentences:");
int n = Integer.parseInt(br.readLine());
System.out.println("Enter the sentence:");
String str = br.readLine();
str = str.trim();
String str1 = "";
for (int i = 0; i < str.length(); i++)//Loop to remove characters other than letters & whitespace
{
char ch = str.charAt(i);
if (Character.isLetter(ch) || Character.isWhitespace(ch)) {
str1 = str1 + ch;
}
}
int count = 0;
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) == ' ') {
count++;
}
}
String tok[] = new String[count + 1];// Array to store tokens of sentence
str1 = str1.trim();
str1 = str1 + ' ';
int x = 0;
StringTokenizer st = new StringTokenizer(str1);
while (st.hasMoreTokens())//Loop to store tokens in array
{
tok[x] = st.nextToken();
x++;
}
System.out.println("Sentences after reversing without punctuation");
for (int i = tok.length - 1; i >= 0; i--) {
System.out.print(tok[i] + " ");
}
}
}