Skip to content

Commit

Permalink
reverse words in string
Browse files Browse the repository at this point in the history
  • Loading branch information
WaderManasi committed Sep 13, 2020
1 parent aeb4ae4 commit be59ad4
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions String/reverse_words.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//You are given a string s. You need to reverse the string.
//solution:

#include<string>
#include<bits/stdc++.h>
using namespace std;

string reverseWords(string s)
{
stack<char>st;
string news="";
int i=s.length()-1;
while(i>=0){
if(s[i]!='.')
{
st.push(s[i]);
}
else {
while(!st.empty())
{
char ch=st.top();
news.push_back(ch);
st.pop();
}
news.push_back('.');
}
i--;
}
while(!st.empty())
{
char ch=st.top();
news.push_back(ch);
st.pop();
}
return news;
}
int main()
{
string str;
cin>>str;

cout<<reverseWords(str)<<endl;
return 0;
}

0 comments on commit be59ad4

Please sign in to comment.