From 6620f32d9c79477ddb3e38ee390a81e16c35a79a Mon Sep 17 00:00:00 2001 From: RK-Shandilya <89962501+RK-Shandilya@users.noreply.github.com> Date: Wed, 14 Dec 2022 23:56:00 +0530 Subject: [PATCH] chore(CPlusPlus): add reverse the string wordwise (#1100) --- algorithms/CPlusPlus/README.md | 1 + .../Strings/ReverseTheStringWordwise.cpp | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 algorithms/CPlusPlus/Strings/ReverseTheStringWordwise.cpp 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<