470,855 Members | 1,318 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

Reverse and print a string

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));
}
}

int main (int argc, char* argv[]) {
char* miastringa="CIAO";
invstr(miastringa);
system("PAUSE"); /* Because of Dev-C++ IDE */
return 0;
}

/* Code ends here */
Jun 27 '08 #1
12 1764
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

Jun 27 '08 #2
nembo kid wrote:
How I (recursive) can reverse and print a string (as simple as
possible)?
[ ... ]

/* Print a string in reverse. Invoke as: Program string */
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
const long len = strlen(argv[1]);
long ctr;

for (ctr = len-1; ctr >= 0; ctr--) putc(argv[1][ctr], stdout);
putc('\0', stdout);
return 0;
}

Jun 27 '08 #3
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'

Jun 27 '08 #4
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?

Jun 27 '08 #5
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.

Jun 27 '08 #6
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/>

Jun 27 '08 #7
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.
Jun 27 '08 #8
santosh ha scritto:
No. See my other reply.
Now all it's clear to me.
Jun 27 '08 #9
santosh ha scritto:
No. See my other reply. And do read the c.l.c FAQ at:
Thanks again, of course ;-)
Jun 27 '08 #10
santosh said:
nembo kid wrote:
>How I (recursive) can reverse and print a string (as simple as
possible)?
[ ... ]

/* Print a string in reverse. Invoke as: Program string */
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
const long len = strlen(argv[1]);
long ctr;

for (ctr = len-1; ctr >= 0; ctr--) putc(argv[1][ctr], stdout);
putc('\0', stdout);
return 0;
}
How is that recursive?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #11
Richard Heathfield wrote:
santosh said:
>nembo kid wrote:
>>How I (recursive) can reverse and print a string (as simple as
possible)?
[ ... ]

/* Print a string in reverse. Invoke as: Program string */
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
const long len = strlen(argv[1]);
long ctr;

for (ctr = len-1; ctr >= 0; ctr--) putc(argv[1][ctr], stdout);
putc('\0', stdout);
return 0;
}

How is that recursive?
I wonder how I missed that when I read it and quoted it too!? Never
mind, the OP has got it right himself.

Incidentally just today I made a silly error (using **p instead of ***p)
that made me waste about an hour hunting around for the cause of the
apparently random output. Just my luck too that the wrong deference did
not segfault. I should probably take a break from Usenet for a while.

Jun 27 '08 #12
On Fri, 02 May 2008 00:31:17 +0530, santosh <sa*********@gmail.comwrote:
>Incidentally just today I made a silly error (using **p instead of ***p)
I'm going to be kind to everybody here and not post my function that
allocates and returns a three-dimensional array. Still, it *is* fun to
scare people with: uchar ****get3D...

--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Jun 27 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Raymond Hettinger | last post: by
59 posts views Thread by Raymond Hettinger | last post: by
13 posts views Thread by Brad Tilley | last post: by
10 posts views Thread by aatish19 | last post: by
7 posts views Thread by analoveu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.