Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   String reversing problem (http://www.velocityreviews.com/forums/t440696-string-reversing-problem.html)

Albert 12-30-2005 03:25 AM

String reversing problem
 
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[i]);
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[i] = s[j];
}

output:

olleh

?


Artie Gold 12-30-2005 03:52 AM

Re: String reversing problem
 
Albert wrote:
> 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[i]);
> 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[i] = s[j];
> }
>
> output:
>
> olleh
>
> ?
>

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"

Diptendra 12-30-2005 03:56 AM

Re: String reversing problem
 
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[i]);
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[i] = s[j];



}


Albert 12-30-2005 03:57 AM

Re: String reversing problem
 
What do you mean by 'desk checking'?


slebetman@yahoo.com 12-30-2005 04:46 AM

Re: String reversing problem
 
Albert wrote:
> 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.


slebetman@yahoo.com 12-30-2005 04:58 AM

Re: String reversing problem
 
Albert wrote:
> Why doesn't:
>
> <snip>
> s[i] = 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'.


pai 12-30-2005 05:41 AM

Re: String reversing problem
 
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


Richard Heathfield 12-30-2005 05:43 AM

Re: String reversing problem
 
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Chuck F. 12-30-2005 07:07 AM

Re: String reversing problem
 
pai wrote:
>
> 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/>

haroon 12-30-2005 01:35 PM

Re: String reversing problem
 

Albert wrote:
> 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[i] = 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[i];
s[i] = s[j];
s[j] = t;
}
}

/***/

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



All times are GMT. The time now is 01:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.