chore(CPlusPlus): add reverse the string wordwise (#1100)
parent
e60d299077
commit
6620f32d9c
|
@ -136,6 +136,7 @@
|
||||||
- [Longest common prefix](Strings/longest-common-prefix.cpp)
|
- [Longest common prefix](Strings/longest-common-prefix.cpp)
|
||||||
- [First unique character in the string](Strings/first-unique-character.cpp)
|
- [First unique character in the string](Strings/first-unique-character.cpp)
|
||||||
- [Sliding Window to match target string](Strings/sliding-window.cpp)
|
- [Sliding Window to match target string](Strings/sliding-window.cpp)
|
||||||
|
- [Reverse String Wordwise](Strings/ReverseTheStringWordwise.cpp)
|
||||||
|
|
||||||
## Trees
|
## Trees
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
// Description :- Given a string, the task is to reverse the order of the words in the given string.
|
||||||
|
// Example :-
|
||||||
|
// Input 1:
|
||||||
|
// A = "the sky is blue"
|
||||||
|
// Input 2:
|
||||||
|
// A = "this is ib"
|
||||||
|
// Output 1:
|
||||||
|
// "blue is sky the"
|
||||||
|
// Output 2:
|
||||||
|
// "ib is this"
|
||||||
|
|
||||||
|
|
||||||
|
// Time Complexity = O(N), Space Complexity = O(N)
|
||||||
|
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
string solve(string s) {
|
||||||
|
vector<string>v;
|
||||||
|
string str="";
|
||||||
|
for(int i=0;i<s.length();i++){
|
||||||
|
if(s[i]!=' '){
|
||||||
|
str+=s[i];
|
||||||
|
}
|
||||||
|
else if(str!="" && s[i]==' '){
|
||||||
|
v.push_back(str);
|
||||||
|
str="";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(str!=""){
|
||||||
|
v.push_back(str);
|
||||||
|
}
|
||||||
|
str="";
|
||||||
|
for(int i=v.size()-1;i>0;i--){
|
||||||
|
str+=v[i];
|
||||||
|
str+=' ';
|
||||||
|
}
|
||||||
|
str+=v[0];
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
string s;
|
||||||
|
getline(cin, s);
|
||||||
|
cout<<solve(s);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue