Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Reverse and print a string

Reply
Thread Tools

Re: Reverse and print a string

 
 
Chris Dollin
Guest
Posts: n/a
 
      05-01-2008
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

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      05-01-2008
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.

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      05-01-2008
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/>

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      05-01-2008
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reverse string, how to print string and not decimals? ssecorp C Programming 47 08-08-2008 06:48 PM
Re: Reverse and print a string santosh C Programming 2 05-02-2008 10:45 PM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
Stacks Queues Reverse Reverse Polish dogbite C++ 4 10-10-2003 05:06 AM



Advertisments