forked from reachanihere/Data-Structures-and-Algorithms
/
Reverse Word Wise.cpp
57 lines (43 loc) · 1.19 KB
/
Reverse Word Wise.cpp
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
/*
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.
Sample Input:
Welcome to Coding Ninjas
Sample Output:
Ninjas Coding to Welcome
*/
// input - given string
// You need to update in the given string itself. No need to print or return anything
#include <bits/stdc++.h>
using namespace std;
void reverseStringWordWise(char input[]) {
// Write your code here
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());
}
#include <iostream>
#include "solution.h"
using namespace std;
int main() {
char input[1000];
cin.getline(input, 1000);
reverseStringWordWise(input);
cout << input << endl;
}