Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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;
}
Comment

how to reverse a string in c++

string reverse(string str)
{
    string output;
    
    for(int i=str.length()-1; i<str.length(); i--)
    {
        output += str[i];
    }
    return output;
}
Comment

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 << "
";

	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to get hcf of two number in c++ 
Cpp :: how can we create 4 digit random number in c++ 
Cpp :: sort c++ 
Cpp :: c++ if example 
Cpp :: function overloading in c++ 
Cpp :: C++ New Lines 
Cpp :: matrix dynamic memory c++ 
Cpp :: how to print in new lines in C++ 
Cpp :: vector library c++ 
Cpp :: joining two vectors in c++ 
Cpp :: c++ elif 
Cpp :: attention nlp 
Cpp :: c++ tuple 
Cpp :: cpp template 
Cpp :: c++ print text 
Cpp :: unpack tuple c++ 
Cpp :: calculator in cpp 
Cpp :: online ide c++ 
Cpp :: how to make dictionary of numbers in c++ 
Cpp :: fill two dimensional array c++ 
Cpp :: loop execution decending order in c 
Cpp :: min heap stl 
Cpp :: glfw error: the glfw library is not initialized 
Cpp :: remove something from stringstream 
Cpp :: how to declare an enum variable c++ 
Cpp :: error uploading arduino code 
Cpp :: c++ if else example 
Cpp :: use set to get duplicates in c++ 
Cpp :: c++ class constructor variable arguments 
Cpp :: flutter text direction auto 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =