xxxxxxxxxx
#include<iostream>
#include<stack>
using namespace std;
string reverseWords(string S)
{
stack<char> s;
string ans;
for (int i = S.length()-1; i >= 0; i--)
{
if (s.empty())
{
s.push(S[i]);
}
else if (!(s.empty()))
{
if (s.top() == '.')
{
s.pop();
while (!(s.empty()))
{
ans += s.top();
s.pop();
}
ans += '.';
}
s.push(S[i]);
}
}
while (s.size())
{
ans += s.top();
s.pop();
}
return ans;
}
int main()
{
string s;
cout << "Enter string: ";
cin >> s;
string result;
result = reverseWords(s);
cout << "result: " << result << "\n";
return 0;
}
xxxxxxxxxx
string reverse(string str)
{
string output;
for(int i=str.length()-1; i<str.length(); i--)
{
output += str[i];
}
return output;
}
xxxxxxxxxx
#include <iostream>
//The library below must be included for the reverse function to work
#include<bits/stdc++.h>
using namespace std;
int main() {
string greeting = "Hello";
//Note that it takes the iterators to the start and end of the string as arguments
reverse(greeting.begin(),greeting.end());
cout<<greeting<<endl;
}