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. Advertising

  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. Advertising

  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. 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. dogbite
    Replies:
    4
    Views:
    694
    osmium
    Oct 10, 2003
  2. keto
    Replies:
    0
    Views:
    968
  3. santosh

    Re: Reverse and print a string

    santosh, May 1, 2008, in forum: C Programming
    Replies:
    2
    Views:
    296
    Kevin D. Quitt
    May 2, 2008
  4. ssecorp
    Replies:
    47
    Views:
    1,064
    Default User
    Aug 8, 2008
  5. David Cournapeau

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

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

Share This Page