Skip to content

Commit 7df043e

Browse files
committed
Added reverse words in a sentence
1 parent 6d314c9 commit 7df043e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Java/ReverseWordInSentence.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package Set1;
2+
3+
import java.util.*;
4+
5+
public class ReverseWordInSentence {
6+
7+
public static String reverse(String value) {
8+
int len = value.length();
9+
char mychar = value.charAt(0);
10+
if (Character.isDigit(mychar)) {
11+
return value;
12+
13+
}
14+
String rev = "";
15+
for (int i = len - 1; i >= 0; i--)
16+
rev = rev + value.charAt(i);
17+
return rev;
18+
}
19+
20+
public static void main(String[] args) {
21+
Scanner sc = new Scanner(System.in);
22+
String[] array = new String[20];
23+
System.out.println("*****Start*****");
24+
int count = 0;
25+
try {
26+
System.out.println("Please enter a sentence");
27+
boolean valid = true;
28+
while (true == valid) {
29+
if (array[count] == "\n") {
30+
valid = false;
31+
break;
32+
} else {
33+
array[count] = sc.next();
34+
System.out.print(reverse(array[count]) + " ");
35+
count++;
36+
}
37+
}
38+
} catch (Exception e) {
39+
System.out.println("Bad choice of input. Try again!");
40+
}
41+
for (int i = 0; i < count; i++) {
42+
System.out.print(reverse(array[i]));
43+
}
44+
System.out.println("*****End*****");
45+
sc.close();
46+
}
47+
48+
}

0 commit comments

Comments
 (0)