Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String reversing problem

Reply
Thread Tools

String reversing problem

 
 
Albert
Guest
Posts: n/a
 
      12-30-2005
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

?

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      12-30-2005
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!"
 
Reply With Quote
 
 
 
 
Diptendra
Guest
Posts: n/a
 
      12-30-2005
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];



}

 
Reply With Quote
 
Albert
Guest
Posts: n/a
 
      12-30-2005
What do you mean by 'desk checking'?

 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      12-30-2005
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.

 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      12-30-2005
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'.

 
Reply With Quote
 
pai
Guest
Posts: n/a
 
      12-30-2005
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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-30-2005
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)
 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      12-30-2005
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/>
 
Reply With Quote
 
haroon
Guest
Posts: n/a
 
      12-30-2005

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.

 
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
Reversing a string without using array, classes and reverse function Rubist Rohit Ruby 19 04-23-2011 09:16 PM
Reversing a string Scott Python 23 07-05-2007 08:20 PM
Reversing order of words in a given string Kelly B C Programming 2 04-26-2007 02:52 PM
Reversing a folder path in a string Lindsay C++ 3 07-18-2005 05:09 AM
reversing a string - newbie question netforce C Programming 9 03-08-2004 12:04 AM



Advertisments