forked from avinashbest/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseEachWord.java
More file actions
59 lines (51 loc) · 1.81 KB
/
Copy pathReverseEachWord.java
File metadata and controls
59 lines (51 loc) · 1.81 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
package strings.Assignment;
import java.util.Scanner;
/*Aadil has been provided with a sentence in the form of a string as a function parameter. The task is to implement a function so as to print the sentence such that each word in the sentence is reversed.
Example:
Input Sentence: "Hello, I am Aadil!"
The expected output will print, ",olleH I ma !lidaA".
Input Format:
The first and only line of input contains a string without any leading and trailing spaces. The input string represents the sentence given to Aadil.
Output Format:
The only line of output prints the sentence(string) such that each word in the sentence is reversed.
Constraints:
0 <= N <= 10^6
Where N is the length of the input string.
Time Limit: 1 second
Sample Input 1:
Welcome to Coding Ninjas
Sample Output 1:
emocleW ot gnidoC sajniN
Sample Input 2:
Always indent your code
Sample Output 2:
syawlA tnedni ruoy edoc*/
public class ReverseEachWord {
public static String reverseString(String str, int start, int end) {
String reverse = "";
while (start < end) {
reverse = str.charAt(start) + reverse;
start++;
}
return reverse;
}
public static String reverseEachWord(String str) {
int n = str.length();
int previousSpaceIndex = -1;
String answer = "";
int i = 0;
for (; i < n; i++) {
if (str.charAt(i) == ' ') {
answer += (reverseString(str, previousSpaceIndex + 1, i) + " ");
previousSpaceIndex = i;
}
}
answer += (reverseString(str, previousSpaceIndex + 1, i) + " ");
return answer;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
System.out.println(reverseEachWord(str));
}
}