-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRevrseStringWordWise.java
More file actions
60 lines (54 loc) · 1.67 KB
/
RevrseStringWordWise.java
File metadata and controls
60 lines (54 loc) · 1.67 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
60
// Reverse String Wordwise
// Send Feedback
// 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
import java.util.Scanner;
public class RevrseStringWordWise {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the string here please : ");
String str = s.nextLine();
if (str == null) {
str = "";
}
String ans = reverseWordWise(str);
System.out.println(ans);
s.close();
}
public static String reverseWordWise(String str) {
String ans = "";
int start = 0;
int end = str.length();
for (int i = str.length() - 1; i >= 0; i--) {
char ch = str.charAt(i);
// Here fetching words and adding them to ans
if (ch == ' ') {
start = i + 1;
for (int j = start; j < end; j++) {
ans += str.charAt(j);
}
end = i;
ans += ' ';
}
}
// Adding first word to ans here
for (int i = 0; i < end; i++) {
ans += str.charAt(i);
}
return ans;
}
}