strings stl

A

adrian suri

HI

does anyone know of a quike way to reverse an stl string




#include <Iostream>
using namespace std;
#include<string>
#include<cstdlib> // includes clearscreen function exit etc

string firstName;
cout<< "Please enter your first name" <<endl;
cin>>firstName;
firstnameLeng=firstName.size();
firstName.reverse() // does not work

adrian
 
R

Rolf Magnus

adrian said:
HI

does anyone know of a quike way to reverse an stl string

Yes. std::reverse().
#include <Iostream>

That must be:

#include said:
using namespace std;
#include<string>
#include<cstdlib> // includes clearscreen function exit etc

#include said:
string firstName;
cout<< "Please enter your first name" <<endl;
cin>>firstName;
firstnameLeng=firstName.size();
firstName.reverse() // does not work

std::reverse(firstName.begin(), firstName.end());
 
A

adrian suri

Hi

thanks for the fast reply, one more question, is there anyway to have
the new reversed string assigned to another string

const string message("my message will never be changed");
string revstr // create a new string
revstr=reverse(message.begin(), message.end()); // does not work but
// this is the type of syntax I am looking for, I am using watcom 1.3
//with stl port, so it works without the std::reverse

adrian

thanks
 
P

Peter Julian

adrian suri said:
Hi

thanks for the fast reply, one more question, is there anyway to have
the new reversed string assigned to another string

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
const std::string message("my message will never be changed");
std::string s_reversed(message);
std::reverse(s_reversed.begin(), s_reversed.end());

std::cout << message << "\n";
std::cout << s_reversed << "\n";

return 0;
}
 
A

adrian suri

Hi thanks Peter


adrian


Peter said:
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
const std::string message("my message will never be changed");
std::string s_reversed(message);
std::reverse(s_reversed.begin(), s_reversed.end());

std::cout << message << "\n";
std::cout << s_reversed << "\n";

return 0;
}
 
P

Pete Becker

Peter said:
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
const std::string message("my message will never be changed");
std::string s_reversed(message);
std::reverse(s_reversed.begin(), s_reversed.end());

std::cout << message << "\n";
std::cout << s_reversed << "\n";

return 0;
}

Also consider

std::string s_reversed(message.rbegin(), message.rend());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,282
Latest member
Xyprime

Latest Threads

Top