How to reverse an std string

0 votes

When I have the string read in binary numbers, I'm trying to figure out how to reverse the string temp.

istream& operator >>(istream& dat1d, binary& b1)   
{             
    string temp; 

    dat1d >> temp;   
}
Jun 1 in C++ by Nicholas
• 5,020 points
33 views

1 answer to this question.

0 votes

A reverse function is integrated into C++ and can be used to reverse a string. 

This function accepts two parameters:

The start iterator for the string

The string iterator has come to an end.

The following line of code demonstrates how to use this 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;

}
answered Jun 1 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

How to initialize an std::string with a length?

How can I correctly initialise a string if its length is specified at build time? #include <string> int length = 3; string word[length]; //invalid ...READ MORE

Jul 27 in C++ by Nicholas
• 5,020 points
9 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
38 views
0 votes
1 answer

How do I erase an element from std::vector<> by index?

You might perform the following to remove a single element: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); // Deletes the second element (vec[1]) vec.erase(std::next(vec.begin())); Alternatively, to remove many elements at once: // ...READ MORE

answered Jun 10 in C++ by Damon
• 4,960 points
41 views
0 votes
1 answer

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

answered Jun 20 in C++ by Damon
• 4,960 points
43 views
0 votes
1 answer

Easiest way to convert int to string in C++

The C++11 version has introduced the std::stoi ...READ MORE

answered Feb 10 in Others by Rahul
• 9,000 points
39 views
0 votes
0 answers

How do you reverse a string in-place in JavaScript?

How do you reverse a string in ...READ MORE

May 23 in Java-Script by Kichu
• 19,040 points
28 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
53 views
0 votes
1 answer

Using getline() in C++

If you use getline() after cin >> anything, you must first flush the newline character from the buffer.  You can achieve this by using the cin.ignore() It would be something like this: string messageVar; cout ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
60 views
0 votes
1 answer

How to convert an instance of std::string to lower case

#include <algorithm> #include <cctype> #include <string> std::string data = "Abc"; std::transform(data.begin(), ...READ MORE

answered Aug 2 in C++ by Damon
• 4,960 points
8 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
41 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP