473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1905
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
13
by: Brad Tilley | last post by:
A friend of mine wrote an algorithm that generates strings. He says that it's impossible to figure out exactly how the algorithm works. That may be true, but I think if I had enough sample strings...
8
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
10
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a...
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
7
by: analoveu | last post by:
I wanna print the following shape 1 121 12321 1234321 12321 121 1
3
by: analoveu | last post by:
I have a problem with using the reverse method any one give me an exmample or correct my error inside this code String p=""; for(int x=0 ; x<=3 ; x++) p+=x; System.out.print(p); String...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.