forked from avinashbest/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseStringWordWise.java
More file actions
51 lines (47 loc) · 1.77 KB
/
ReverseStringWordWise.java
File metadata and controls
51 lines (47 loc) · 1.77 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
package strings;
import java.util.Scanner;
/*Reverse the given string word wise. That is, the last word in given string should come at 1st place, last second word at 2nd place and so on. Individual words should remain as it is.
Input format
String in a single line
Output format
Word wise reversed string in a single line
Constraints
0 = S = 10^7
where S represents the length of string, S.
Sample Input 1
Welcome to Coding Ninjas
Sample Output 1
Ninjas Coding to Welcome
Sample Input 2
Always indent your code
Sample Output 2
code your indent Always*/
public class ReverseStringWordWise {
public static String reverseStringWordWise(String str) {
String answer = "";
// 2 pointer pointing at the last character of the String
int i = str.length() - 1/*to traverse the whole string in reverse order*/, j = str.length() - 1/*to mark the position if the first pointer i hits a space*/;
for (; i >= 0; i--) {
if (str.charAt(i) == ' ') {
String reverseWord = "";
/*Copying the character to answer string*/
for (int k = i + 1; k <= j; k++) {
reverseWord += str.charAt(k);
}
answer += reverseWord + " ";
/*2nd pointer will be assigned to the next limit for till which the character in answer string will be copied*/
j = i - 1;
}
}
/*handling the last word case because it don't have any space*/
for (int k = i + 1; k <= j; k++) {
answer += str.charAt(k);
}
return answer;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
System.out.println(reverseStringWordWise(str));
}
}