Reverse String

Discussion in 'C++' started by HELLO $$$, Sep 23, 2006.

  1. HELLO  $$$

    HELLO $$$ Guest

    From Beginner [ C++ ]:
    I am studying a C++ program, using: char, arrays, etc.
    The aim of this program is to let it make "Reverse" of any statement
    (string) as example:
    if the original string is " this is test " ; the output of the program is
    reversing this statement to be " tset si siht ".
    so the string became reversed.
    I am asking: 1- why some of programmers do this ?
    2- What's the benefit if they do this ? at any circumstances?
    Thank to all.
    ------------------------------------------------------
     
    HELLO $$$, Sep 23, 2006
    #1
    1. Advertising

  2. HELLO $$$ wrote:

    > I am asking: 1- why some of programmers do this ?


    Because someone or some manual tell him to do.

    > 2- What's the benefit if they do this ? at any circumstances?


    They have a bigger probability of pass the course if they do.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Sep 23, 2006
    #2
    1. Advertising

  3. HELLO  $$$

    HELLO $$$ Guest

    I mean what's the practical benefit which reflect on him practically .
    ----------------------------------------------------------
    "Julián Albo" <> wrote in message
    news:...
    > HELLO $$$ wrote:
    >
    >> I am asking: 1- why some of programmers do this ?

    >
    > Because someone or some manual tell him to do.
    >
    >> 2- What's the benefit if they do this ? at any circumstances?

    >
    > They have a bigger probability of pass the course if they do.
    >
    > --
    > Salu2
     
    HELLO $$$, Sep 23, 2006
    #3
  4. HELLO $$$ posted:

    > I mean what's the practical benefit which reflect on him practically.


    He/She gets practise at working with algorithms. It's actually quite a
    beneficial exercise. I'll give it a quick go without using the Standard
    Library for the craic:

    template<class T>
    inline void SwapObjects(T &a, T&b)
    {
    T const temp = a;
    a = b;
    b = temp;
    }

    template<class T>
    inline void ReverseArray(T *pstart, T *pend)
    {
    do swap(*pstart++,*pend--);
    while(pstart < pend);
    }

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 23, 2006
    #4
  5. HELLO  $$$

    osmium Guest

    "HELLO $$$" writes:

    > From Beginner [ C++ ]:
    > I am studying a C++ program, using: char, arrays, etc.
    > The aim of this program is to let it make "Reverse" of any statement
    > (string) as example:
    > if the original string is " this is test " ; the output of the program is
    > reversing this statement to be " tset si siht ".
    > so the string became reversed.
    > I am asking: 1- why some of programmers do this ?
    > 2- What's the benefit if they do this ? at any circumstances?


    It's simple a student exercise intended to make you "think like a
    programmer". The cases where a real programmer would do such a thing are
    probably quite rare.

    If you were studying algebra, you would accept those (drill) equations to
    solve without knowing what the underlying need is, wouldn't you? A very
    similar situation here.
     
    osmium, Sep 23, 2006
    #5
  6. Frederick Gotham posted:

    > T const temp = a;



    Arguably, I should have written that with parentheses rather than an =
    symbol.

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 23, 2006
    #6
  7. HELLO  $$$

    loufoque Guest

    Frederick Gotham wrote :

    > do swap(*pstart++,*pend--);


    Did you mean do SwapObjects(*pstart++,*pend--); ?
     
    loufoque, Sep 23, 2006
    #7
  8. Frederick Gotham schrieb:
    [why exercises like reversing strings are assigned]
    > He/She gets practise at working with algorithms. It's actually quite a
    > beneficial exercise.


    This exercise also makes familiar with pointers and pointer arithmetics, or
    with arrays and indexes (depends on how it is solved).

    > I'll give it a quick go without using the Standard
    > Library for the craic:
    >
    > template<class T>
    > inline void SwapObjects(T &a, T&b)
    > {
    > T const temp = a;
    > a = b;
    > b = temp;
    > }
    >
    > template<class T>
    > inline void ReverseArray(T *pstart, T *pend)
    > {
    > do swap(*pstart++,*pend--);
    > while(pstart < pend);
    > }


    You should document, what pstart and pend points to (including or excluding
    start and end), because your code accesses *pend, which is a no-no! for
    standard library style iterators that points to the end.

    begin/end iterators pairs are specified by 'begin' referring to the first
    element and 'end' referring to one past the last element in the sequence.

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
     
    Thomas J. Gritzan, Sep 23, 2006
    #8
  9. loufoque posted:

    >> do swap(*pstart++,*pend--);

    >
    > Did you mean do SwapObjects(*pstart++,*pend--); ?



    Yes, I should've written SwapObjects.

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 24, 2006
    #9
  10. Thomas J. Gritzan posted:

    > You should document, what pstart and pend points to (including or excluding
    > start and end), because your code accesses *pend, which is a no-no! for
    > standard library style iterators that points to the end.
    >
    > begin/end iterators pairs are specified by 'begin' referring to the first
    > element and 'end' referring to one past the last element in the sequence.


    I use "p_end" to refer to the last element, and "p_over" to refer to one past
    the last. If I were to write intructions for usage of the function though,
    I'd certainly indicate what pstart and pend refer to.

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 24, 2006
    #10
    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. Curt_C [MVP]
    Replies:
    0
    Views:
    1,915
    Curt_C [MVP]
    Jan 22, 2004
  2. Roedy Green
    Replies:
    9
    Views:
    1,617
    Jeff Schwab
    Aug 11, 2005
  3. dogbite
    Replies:
    4
    Views:
    694
    osmium
    Oct 10, 2003
  4. Rakesh

    String Reverse Question

    Rakesh, Apr 16, 2004, in forum: C Programming
    Replies:
    45
    Views:
    1,334
    Christopher Benson-Manica
    Apr 20, 2004
  5. ssecorp
    Replies:
    47
    Views:
    1,060
    Default User
    Aug 8, 2008
Loading...

Share This Page