forked from shivprime94/Data-Structure-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversewordwise.cpp
More file actions
68 lines (51 loc) · 1.42 KB
/
reversewordwise.cpp
File metadata and controls
68 lines (51 loc) · 1.42 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
61
62
63
64
65
66
67
68
/*Reverse Word Wise
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
*/
//my code
#include <bits/stdc++.h>
using namespace std;
using namespace std;
void reverseStringWordWise(char input[]) { //function to reverse string wordwise
vector <string> words;
string word="";
int size=strlen(input);
for(int i=0;i<=size;i++){
if(input[i]!=' ' && input[i]!='\0')
word+=input[i];
else{
words.push_back(word);
word="";
}
}
reverse(words.begin(),words.end());
string ans="";
for(int i=0;i<words.size();i++){
if(i!=words.size()-1)
ans+=words[i]+" ";
else
ans+=words[i];
}
strcpy(input,ans.c_str());
}
int main() {
char input[1000];
cin.getline(input, 1000);
reverseStringWordWise(input);
cout << input << endl;
}