Answers for "reverse words in string c++"

C++
3

How to reverse a string in c++ using reverse function

#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;
}
Posted by: Guest on August-20-2021
0

Reverse words in a given string solution in c++

#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;
}
Posted by: Guest on March-21-2022

Code answers related to "reverse words in string c++"

Browse Popular Code Answers by Language