In-place function

Discussion in 'C++' started by peter.mcclymont@gmail.com, Apr 3, 2006.

  1. Guest

    Hi There,

    Can you please help me write an in-place function in C++ for reversing
    a string.

    What is an in-place function?

    So what is a non-in-place function in that case to reverse a C++
    string?

    Urgent help needed.

    Thanks,

    Peter.
     
    , Apr 3, 2006
    #1
    1. Advertising

  2. Pete C Guest

    wrote:
    > Can you please help me write an in-place function in C++ for reversing
    > a string.


    #include <string>
    #include <algorithm>

    void strrev(std::string &s) {
    std::reverse(s.begin(), s.end());
    }

    That kind of homework question is sooo 1980's.
     
    Pete C, Apr 3, 2006
    #2
    1. Advertising

  3. Alan Johnson Guest

    wrote:
    > Can you please help me write an in-place function in C++ for reversing
    > a string.


    std::string s = "Hello, world!" ;
    std::reverse(s.begin(), s.end()) ;
     
    Alan Johnson, Apr 3, 2006
    #3
  4. Rolf Magnus Guest

    wrote:

    > Hi There,
    >
    > Can you please help me write an in-place function in C++ for reversing
    > a string.
    >
    > What is an in-place function?
    >
    > So what is a non-in-place function in that case to reverse a C++
    > string?


    I think it's about reversing the string in-place. It means that you don't
    create a new string with the same content in reversed order, but rather you
    modify the original string by exchanging characters.
     
    Rolf Magnus, Apr 3, 2006
    #4
  5. Daniel T. Guest

    In article <>,
    wrote:

    > Hi There,
    >
    > Can you please help me write an in-place function in C++ for reversing
    > a string.


    #include <iostream>
    #include <string>
    // add includes as you think necessary

    using namespace std;

    void reverse( string& s ) {
    // your code here
    }

    int main() {
    string s( "AB" );
    reverse( s );
    assert( s == "BA" );
    cout << "Working\n";
    }

    Cut and paste the above into your compiler. Add code to the "your code
    here" section until 'Working' shows up on your screen when you run it.
    Then post back here with what you did.



    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
     
    Daniel T., Apr 3, 2006
    #5
  6. Tomás Guest

    posted:

    > Hi There,
    >
    > Can you please help me write an in-place function in C++ for reversing
    > a string.
    >
    > What is an in-place function?
    >
    > So what is a non-in-place function in that case to reverse a C++
    > string?
    >
    > Urgent help needed.
    >
    > Thanks,
    >
    > Peter.



    This problem is so trivial that I'd challenge myself as to just how
    efficient I can make it.

    (Yes I realise I'm doing your homework, but this could be a laugh...)

    The following code is unchecked and most likely doesn't work, but what the
    hey:

    #include <cstddef>
    #include <cstring>
    #include <iostream>
    using std::cout; using std::endl;

    /* The actual function */
    void ReverseString( char* p_beginning, char* p_end )
    {
    for (;;)
    {
    std::swap( *p_beginning, *p_end );

    switch (p_end++ - p_beginning++)
    {
    case 1: case 2: return;
    }
    }
    }

    /* The function whose argument is an array */
    template<std::size_t i>
    void ReverseString( char (&p_start) )
    {
    ReverseString( &p_start[0], &p_start[i - 1] );
    }

    /* The function which takes a pointer, and also an integral length */
    void ReverseString( char* const p_str, std::size_t len )
    {
    ReverseString( &p_str[0], &p_str[--len] );
    }

    /* The function which take a pointer */
    void ReverseString( char* const p_start )
    {
    ReverseString( p_start, std::strlen(p_start) );
    }

    int main()
    {
    char monkey[] = "monkey";
    char cow[] = "cow";
    char niagra[] = "niagra";
    char stringent[] = "stringent";

    ReverseString( monkey ); ReverseString(cow); ReverseString(niagra);

    ReverseString( &monkey[0] ); ReverseString( &cow[0] );

    cout << monkey << cow << niagra << stringent;
    }


    -Tomás
     
    Tomás, Apr 3, 2006
    #6
  7. Guest

    Tomás wrote:
    > posted:
    >
    > > Hi There,
    > >
    > > Can you please help me write an in-place function in C++ for reversing
    > > a string.

    >
    > This problem is so trivial that I'd challenge myself as to just how
    > efficient I can make it.
    >
    > (Yes I realise I'm doing your homework, but this could be a laugh...)


    How about this one :D
    Probably gets bonus points from the teacher (or an instant F :D ):

    #include <iostream>
    #include <string>

    std::string strinv( std::string s )
    {
    for( unsigned i=0; i<s.size()/2; ++i )
    s[s.size()-i-1]^=s^=s[s.size()-i-1]^=s;
    return s;
    }

    int main()
    {
    std::string hello( "Hello World!" );
    std::cout << strinv( hello ) << std::endl;
    }

    Cheers,
    Andre
     
    , Apr 3, 2006
    #7
  8. red floyd Guest

    wrote:
    > Tomás wrote:
    >> posted:
    >>
    >>> Hi There,
    >>>
    >>> Can you please help me write an in-place function in C++ for reversing
    >>> a string.

    >> This problem is so trivial that I'd challenge myself as to just how
    >> efficient I can make it.
    >>
    >> (Yes I realise I'm doing your homework, but this could be a laugh...)

    >
    > How about this one :D
    > Probably gets bonus points from the teacher (or an instant F :D ):
    >
    > #include <iostream>
    > #include <string>
    >
    > std::string strinv( std::string s )
    > {
    > for( unsigned i=0; i<s.size()/2; ++i )
    > s[s.size()-i-1]^=s^=s[s.size()-i-1]^=s;
    > return s;
    > }
    >
    > int main()
    > {
    > std::string hello( "Hello World!" );
    > std::cout << strinv( hello ) << std::endl;
    > }



    Come on, we could come up with something more obfuscated than that if we
    really try! :)
     
    red floyd, Apr 3, 2006
    #8
    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. Ian Bicking
    Replies:
    4
    Views:
    344
    Ian Bicking
    Apr 28, 2006
  2. Sori Schwimmer

    Editing a function in-memory and in-place

    Sori Schwimmer, Apr 27, 2006, in forum: Python
    Replies:
    0
    Views:
    300
    Sori Schwimmer
    Apr 27, 2006
  3. hurry
    Replies:
    8
    Views:
    396
    haroon
    Dec 29, 2005
  4. In-place function

    , Apr 3, 2006, in forum: C++
    Replies:
    1
    Views:
    290
  5. Sergio Correia

    Best place for a function?

    Sergio Correia, Mar 7, 2007, in forum: Python
    Replies:
    4
    Views:
    270
    Sergio Correia
    Mar 7, 2007
Loading...

Share This Page