diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 77543aa8..6bd7c105 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -136,6 +136,7 @@ - [Longest common prefix](Strings/longest-common-prefix.cpp) - [First unique character in the string](Strings/first-unique-character.cpp) - [Sliding Window to match target string](Strings/sliding-window.cpp) +- [Reverse String Wordwise](Strings/ReverseTheStringWordwise.cpp) ## Trees diff --git a/algorithms/CPlusPlus/Strings/ReverseTheStringWordwise.cpp b/algorithms/CPlusPlus/Strings/ReverseTheStringWordwise.cpp new file mode 100644 index 00000000..8c221840 --- /dev/null +++ b/algorithms/CPlusPlus/Strings/ReverseTheStringWordwise.cpp @@ -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 +using namespace std; + +string solve(string s) { + vectorv; + string str=""; + for(int i=0;i0;i--){ + str+=v[i]; + str+=' '; + } + str+=v[0]; + return str; +} + +int main() +{ + string s; + getline(cin, s); + cout<