Re: Reverse and print a string

Discussion in 'C Programming' started by Chris Dollin, May 1, 2008.

  1. Chris Dollin

    Chris Dollin Guest

    nembo kid wrote:

    > How I (recursive) can reverse and print a string (as simple as
    > possible)? I tried with the following code, but it doesn't work. Thanks
    > in advance to everbody.
    >
    >
    > /* Code starts here */
    >
    > #include <stdio.h>
    >
    > void invstr(char* s) {
    >
    > /* base case */
    > if ((*s) == '\0')
    > return;
    > else { /* general case */
    > invstr(s++);
    > printf("%c", *(s));
    > }


    `s++` increments `s`. You don't want to do that; you just want
    to pass `s+1`.

    --
    "The whole apparatus had the look of having been put /Jack of Eagles/
    together with the most frantic haste a fanatically
    careful technician could muster."

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
     
    Chris Dollin, May 1, 2008
    #1
    1. Advertisements

  2. Chris Dollin

    santosh Guest

    nembo kid wrote:

    > Chris Dollin ha scritto:
    >
    >> `s++` increments `s`. You don't want to do that; you just want
    >> to pass `s+1`.

    >
    > Oh right! perfect
    >
    > So 's++' it's not same as 's+1'


    No. The postfix increment and decrement operators (i.e., obj++, and
    obj--) modify their operands, which must be an lvalue (i.e., a
    modifiable object). They yield the current value of their operand and
    then increment or decrement, as the case may be, their value. The
    prefix increment and decrement operators (++obj and --obj) also accept
    an lvalue and yield the value of their operand _after_ increment or
    decrement, unlike the postfix versions.

    The expression 's+1' on the other hand does not modify 's'. It merely
    takes the value in 's', and adds 1 to it. So in this program

    int main(void)
    {
    int i = 1, j = 2, k = 3;
    printf("i = %d\nj = %d\nk = %d\n", i+1, j++, ++k);
    return 0;
    }

    Output is:

    i = 1
    j = 2
    k = 4

    after the printf statement is done 'i' will be 1, j will be 3 and k will
    be 4.
     
    santosh, May 1, 2008
    #2
    1. Advertisements

  3. Chris Dollin

    santosh Guest

    nembo kid wrote:

    > nembo kid ha scritto:
    >
    >> So 's++' it's not same as 's+1'

    >
    > I think 's++' is ((address of s)+1)
    > while 's+1' is ((address of s)+1*sizeof(char))
    >
    > Right?


    No. See my other reply. And do read the c.l.c FAQ at:

    <http://www.c-faq.com/>
     
    santosh, May 1, 2008
    #3
  4. Chris Dollin

    santosh Guest

    santosh wrote:

    > nembo kid wrote:
    >
    >> Chris Dollin ha scritto:
    >>
    >>> `s++` increments `s`. You don't want to do that; you just want
    >>> to pass `s+1`.

    >>
    >> Oh right! perfect
    >>
    >> So 's++' it's not same as 's+1'

    >
    > No. The postfix increment and decrement operators (i.e., obj++, and
    > obj--) modify their operands, which must be an lvalue (i.e., a
    > modifiable object). They yield the current value of their operand and
    > then increment or decrement, as the case may be, their value. The
    > prefix increment and decrement operators (++obj and --obj) also accept
    > an lvalue and yield the value of their operand _after_ increment or
    > decrement, unlike the postfix versions.
    >
    > The expression 's+1' on the other hand does not modify 's'. It merely
    > takes the value in 's', and adds 1 to it. So in this program
    >
    > int main(void)
    > {
    > int i = 1, j = 2, k = 3;
    > printf("i = %d\nj = %d\nk = %d\n", i+1, j++, ++k);
    > return 0;
    > }
    >
    > Output is:
    >
    > i = 1


    Sorry, typo. This should actually read i = 2.

    > j = 2
    > k = 4
    >
    > after the printf statement is done 'i' will be 1, j will be 3 and k
    > will be 4.
     
    santosh, May 1, 2008
    #4
    1. Advertisements

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. Bernard Segonnes

    Print and print overview and current page

    Bernard Segonnes, Nov 4, 2004, in forum: Java
    Replies:
    0
    Views:
    565
    Bernard Segonnes
    Nov 4, 2004
  2. dogbite
    Replies:
    4
    Views:
    885
    osmium
    Oct 10, 2003
  3. vijay
    Replies:
    8
    Views:
    946
  4. Assignment: Print reverse order

    , Apr 12, 2006, in forum: C Programming
    Replies:
    10
    Views:
    704
    Wolfgang Riedel
    Apr 13, 2006
  5. keto
    Replies:
    0
    Views:
    1,370
  6. santosh

    Re: Reverse and print a string

    santosh, May 1, 2008, in forum: C Programming
    Replies:
    2
    Views:
    364
    Kevin D. Quitt
    May 2, 2008
  7. ssecorp
    Replies:
    47
    Views:
    1,306
    Default User
    Aug 8, 2008
  8. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    638
    David Cournapeau
    Dec 30, 2008
Loading...