String reversing problem

A

Albert

Why doesn't:

#include <stdio.h>

void reverse(char[], int);

main()
{
char s[5];

s[0] = 'h';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
reverse(s, 5);

for (int i=0; i<=4; i++)
putchar(s);
return 0;
}

void reverse(char s[], int num_elements)
{
int i, j;

for (i=0,j=num_elements-1; (i<=num_elements-1) && (j>=0); i++,j--)
s = s[j];
}

output:

olleh

?
 
A

Artie Gold

Albert said:
Why doesn't:

#include <stdio.h>

void reverse(char[], int);

main()
{
char s[5];

s[0] = 'h';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
reverse(s, 5);

for (int i=0; i<=4; i++)
putchar(s);
return 0;
}

void reverse(char s[], int num_elements)
{
int i, j;

for (i=0,j=num_elements-1; (i<=num_elements-1) && (j>=0); i++,j--)


Think what happens when i==4 and j==1, for example. [Do they still call
it `desk checking'?]
s = s[j];
}

output:

olleh

?

HTH,
--ag
 
D

Diptendra

use this one
#include <stdio.h>


void reverse(char[], int);


main()
{
char s[5];
int i;

s[0] = 'h';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
reverse(s, 5);


for ( i = 0; i<=4; i++)
putchar(s);
return 0;



}


void reverse(char s[], int num_elements)
{
int i, j;

for (i=0,j=num_elements-1; (i<=num_elements-1) && (j>=0); i++,j--)
s = s[j];



}
 
S

slebetman

Albert said:
What do you mean by 'desk checking'?

I think he means checking by pen & paper. I'd call it "checking by pen
& paper" although I always use a whiteboard for it.

Computers can only do what you tell them to do. As they say: garbage
in, garbage out. Sometimes when the computer doesn't do what you want
it is worth checking if you told it to do what you thought you wanted.
 
S

slebetman

Albert said:
Why doesn't:

<snip>
s = s[j];


This is a good example of why desk-checking is good. Lets take a
"hello" string shall we? Note that in the "diagram" below I mark the
variable being "read" from with + and the variable being "written" to
with ^.

h e l l o // original string

o e l l o // doing s[0] = s[4]
^ +

o l l l o // doing s[1] = s[3]
^ +

o l l l o // doing s[2] = s[2]
^

o l l l o // doing s[3] = s[1]
+ ^

At this point I hope you see the problem with you code since you've
overwritten the original 'e' with an 'l'.
 
P

pai

hi ,
I think an extra variable is needed to store, bcoz u cant
interchange 2 variable as such.
or is there any way to do it ...
Pai
 
R

Richard Heathfield

pai said:
hi ,
I think an extra variable is needed to store, bcoz u cant
interchange 2 variable as such.
or is there any way to do it ...

There is a way to do this under certain conditions, but it's not a very
bright idea.
 
C

Chuck F.

pai said:
I think an extra variable is needed to store, bcoz u cant
interchange 2 variable as such. or is there any way to do it
Include context, without which your message is meaningless. For
means on the broken google interface, see my sig below.

Try this, after #include <string.h>:

/* reverse string in place. Return length */
static size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

if ((lgh = strlen(stg)) > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
H

haroon

Albert said:
Why doesn't: [...]
void reverse(char s[], int num_elements)
{
int i, j;

for (i=0,j=num_elements-1; (i<=num_elements-1) && (j>=0); i++,j--)
s = s[j];
}


try writing reverse(...) like this:

/***/
void reverse(char s[], int num_elements)
{
int i, j;
char t;

for (i=0,j=num_elements-1; (i<=(num_elements-1) / 2) && (j>=0);
i++,j--)
{
t = s;
s = s[j];
s[j] = t;
}
}

/***/

then analyze both to figure out whats the difference and what happend.
 
T

tmp123

Chuck said:
Include context, without which your message is meaningless. For
means on the broken google interface, see my sig below.

Try this, after #include <string.h>:

/* reverse string in place. Return length */
static size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

if ((lgh = strlen(stg)) > 1) {
last = stg + lgh; /* points to '\0' */
while (last-- > stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

Hi,

I agree on the previous, and, of course, if some day I need to code
something similar I will write more or less the same (specially in an
answer to a beginner).

But, just for fun, and taken into account is the third time this
question has been posted, another version:

int revstring ( char *s )
{
char *e;
int r;
for( e=s+(r=strlen(s))-1; s<e; *s^=*e^=*s^=*e, s++, e--);
return r;
}


Kind regards.

(hope google doesn't heats indentation).
 
W

William J. Leary Jr.

Albert said:
What do you mean by 'desk checking'?

I'd have called it "paper check," or, when I'm feeling whimsical "let's play
computer."

Whatever way it's expressed, it means get out some paper and a pen(cil) and
perform, yourself, the steps the computer will take to execute your program.
Sometimes it's better (or quicker) than a debugger. Sometimes it's the only
way to debug (for very limited platforms, for example). Well worth practicing.

- Bill
 
C

Chuck F.

tmp123 said:
Chuck F. wrote:
.... snip ...

.... snip ...

But, just for fun, and taken into account is the third time this
question has been posted, another version:

int revstring ( char *s )
{
char *e;
int r;
for( e=s+(r=strlen(s))-1; s<e; *s^=*e^=*s^=*e, s++, e--);
return r;
}

FYI your version invokes undefined behaviour. Try it with a string
of zero chars. My length test is not there for fun. I also have
evil suspicions about the xor operations.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
T

tmp123

Chuck said:
FYI your version invokes undefined behaviour. Try it with a string
of zero chars. My length test is not there for fun. I also have
evil suspicions about the xor operations.

Hi,

If length is 0, then e=s-1, thus s<e is false exiting loop.

However, if the usage of pointer comparation and pointer substraction
is not welcome, another version:

void revstring ( char *s )
{
char *e;
for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=*e);
}

Kind regards.

PS: some days ago, someone posted about if "C is easy". I do not known,
but it is tricky.
 
K

Keith Thompson

tmp123 said:
void revstring ( char *s )
{
char *e;
for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=*e);
}

Undefined behavior.
PS: some days ago, someone posted about if "C is easy". I do not known,
but it is tricky.

It can be if you go out of your way to make it tricky.
 
T

Tim Rentsch

tmp123 said:
Could be... but, could you prove your statement?

Even the first half of the control expression (namely, 's!=e--')
can yield undefined behavior if strlen(s) == 0.
 
C

Christian Bau

"tmp123 said:
Could be... but, could you prove your statement?

It's kind of obvious. Even if it wasn't, whoever wrote that kind of code
should be slapped silly. Immediate removal from any programming team
that I am involved with.
 
C

Chuck F.

tmp123 said:
If length is 0, then e=s-1, thus s<e is false exiting loop.

However, if the usage of pointer comparation and pointer
substraction is not welcome, another version:
void revstring ( char *s )
{
char *e;
for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=*e);
}

Same problem and undefined behaviour. You are not allowed to
generate a pointer that points before s (although you are allowed
to generate one that points just after s).

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top