-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathReverseTheString.java
More file actions
51 lines (32 loc) · 960 Bytes
/
ReverseTheString.java
File metadata and controls
51 lines (32 loc) · 960 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
48
49
50
/**
Given a string A.
Return the string A after reversing the string word by word.
NOTE:
A sequence of non-space characters constitutes a word.
Your reversed string should not contain leading or trailing spaces, even if it is present in the input string.
If there are multiple spaces between words, reduce them to a single space in the reversed string.
Input Format
The only argument given is string A.
Output Format
Return the string A after reversing the string word by word.
For Example
Input 1:
A = "the sky is blue"
Output 1:
"blue is sky the"
Input 2:
A = "this is ib"
Output 2:
"ib is this"
**/
public class ReverseTheString {
public String solve(String A) {
String[] sentence = A.split("\\s+");
String solution = "";
for(int i=0; i<sentence.length; i++)
{
solution+=sentence[sentence.length - i - 1] + " ";
}
return solution.trim();
}
}