Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inverting a string

Reply
Thread Tools

inverting a string

 
 
Gary Wessle
Guest
Posts: n/a
 
      08-16-2006
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;

}

 
Reply With Quote
 
 
 
 
Gary Wessle
Guest
Posts: n/a
 
      08-17-2006
Gary Wessle <> writes:

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


 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      08-18-2006
In article <>, 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inverting custom cursor Niels Dybdahl Java 0 11-21-2005 09:07 AM
Inverting a Regular Expression in Validator spalding ASP .Net 1 01-28-2005 05:46 PM
Inverting a Regular Expression in Validator MWells ASP .Net 1 01-28-2005 02:44 AM
In search of elegant code: inverting a string David Filmer Perl Misc 9 10-30-2003 08:00 PM
question on inverting text -> txet arctan Perl 6 10-16-2003 09:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57