471,084 Members | 801 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,084 software developers and data experts.

inverting a string

hi

I tried to do this, something is not correct.

#include <iostream>
#include <string>
using namespace std;
string inverse(string s)
{

// string inv = "";
// int i = s.size(); // reinterpret_cast no go
// while(i>0){
// inv += s.substr(i,1);
// i--;
// }

string inv = "";
string::iterator begin = s.begin();
string::iterator end = s.end();
(while begin != end){
inv += *begin;
}

}

int main(){

string k = "IRS";
cout << inverse(k) << endl;

}

Aug 18 '06 #1
2 5355
Gary Wessle <ph****@yahoo.comwrites:
hi

I tried to do this, something is not correct.

#include <iostream>
#include <string>
using namespace std;
string inverse(string s)
{

// string inv = "";
// int i = s.size(); // reinterpret_cast no go
// while(i>0){
// inv += s.substr(i,1);
// i--;
// }

string inv = "";
string::iterator begin = s.begin();
string::iterator end = s.end();
(while begin != end){
inv += *begin;
}

}

int main(){

string k = "IRS";
cout << inverse(k) << endl;

}
#include <algorithm>
string str = "sunny";
reverse(str.begin(), str.end())
cout << str << endl;
Aug 18 '06 #2
In article <87************@yahoo.com>, ph****@yahoo.com says...

[ ... ]
string inverse(string s)
The library already provides enough to let you do this in one line of
code quite easily. First of all, when you want to traverse some
collection (and for this purpose, a string qualifies as a collection) in
reverse, you usually want to use a reverse_iterator. Most collections
provide begin() and end(). Those that support reverse_iterators normally
suppose rbegin() and rend() as well.

std::string also has a ctor that takes a pair of iterators as arguments
to specify the contents of the new string.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Krakatioison | last post: by
5 posts views Thread by Stu Cazzo | last post: by
9 posts views Thread by John F Dutcher | last post: by
9 posts views Thread by Derek Hart | last post: by
6 posts views Thread by arctan | last post: by
10 posts views Thread by Angus Leeming | last post: by
1 post views Thread by spalding | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.