-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseString.java
28 lines (25 loc) · 940 Bytes
/
ReverseString.java
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
import java.util.Scanner;
import java.util.StringTokenizer;
public class ReverseString {
public static String reverseWords(String s) {
StringTokenizer tokenizer = new StringTokenizer(s);
StringBuilder builder = new StringBuilder() ;
while (tokenizer.hasMoreTokens()) {
String tokeString = tokenizer.nextToken();
builder.append(reverse(tokeString)).append(" ");
}
return builder.toString().substring(builder.toString().length()-1);
}
public static String reverse(String s){
char [] str = s.toCharArray();
StringBuilder builder = new StringBuilder() ;
for (int i = str.length-1; i >=0; i--) {
builder.append(str[i]);
}
return builder.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(reverseWords(sc.nextLine()));
}
}