inverting a string

Discussion in 'C++' started by Gary Wessle, Aug 17, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    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;

    }
    Gary Wessle, Aug 17, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Gary Wessle Guest

    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;
    Gary Wessle, Aug 17, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Jerry Coffin Guest

    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.
    Jerry Coffin, Aug 18, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. arctan
    Replies:
    6
    Views:
    1,026
    arctan
    Oct 16, 2003
  2. MWells
    Replies:
    1
    Views:
    645
    spalding
    Jan 28, 2005
  3. spalding
    Replies:
    1
    Views:
    411
    Peter Blum
    Jan 28, 2005
  4. Niels Dybdahl

    inverting custom cursor

    Niels Dybdahl, Nov 21, 2005, in forum: Java
    Replies:
    0
    Views:
    399
    Niels Dybdahl
    Nov 21, 2005
  5. David Filmer

    In search of elegant code: inverting a string

    David Filmer, Oct 29, 2003, in forum: Perl Misc
    Replies:
    9
    Views:
    121
    Russ Jones
    Oct 30, 2003
Loading...

Share This Page