Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

String reversing problem

 
 
tmp123
Guest
Posts: n/a
 
      12-30-2005
Chuck F. wrote:
> 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/>


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).

 
Reply With Quote
 
 
 
 
William J. Leary Jr.
Guest
Posts: n/a
 
      12-30-2005
"Albert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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


 
Reply With Quote
 
 
 
 
Chuck F.
Guest
Posts: n/a
 
      12-30-2005
tmp123 wrote:
> Chuck F. wrote:
>

.... snip ...
>
>> /* 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 */

>

.... 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/>
 
Reply With Quote
 
tmp123
Guest
Posts: n/a
 
      12-30-2005
Chuck F. wrote:
> tmp123 wrote:
> > 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.
>


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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-30-2005
"tmp123" <(E-Mail Removed)> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
tmp123
Guest
Posts: n/a
 
      12-30-2005

Keith Thompson wrote:
> "tmp123" <(E-Mail Removed)> writes:
> [...]
> > void revstring ( char *s )
> > {
> > char *e;
> > for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=*e);
> > }

>
> Undefined behavior.
>


Could be... but, could you prove your statement?

 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      12-30-2005
"tmp123" <(E-Mail Removed)> writes:

> Keith Thompson wrote:
> > "tmp123" <(E-Mail Removed)> writes:
> > [...]
> > > void revstring ( char *s )
> > > {
> > > char *e;
> > > for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=*e);
> > > }

> >
> > Undefined behavior.
> >

>
> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-30-2005
"tmp123" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> "tmp123" <(E-Mail Removed)> writes:
>> [...]
>> > void revstring ( char *s )
>> > {
>> > char *e;
>> > for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=*e);
>> > }

>>
>> Undefined behavior.
>>

>
> Could be... but, could you prove your statement?


The expression
*s^=*e^=*s++^=*e
modifies both *s and *e twice between sequence points.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      12-30-2005
In article <(E-Mail Removed) .com>,
"tmp123" <(E-Mail Removed)> wrote:

> Keith Thompson wrote:
> > "tmp123" <(E-Mail Removed)> writes:
> > [...]
> > > void revstring ( char *s )
> > > {
> > > char *e;
> > > for( e=s+strlen(s); s!=e-- && s!=e; *s^=*e^=*s++^=*e);
> > > }

> >
> > Undefined behavior.
> >

>
> 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.
 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      12-30-2005
tmp123 wrote:
> Chuck F. wrote:
>> tmp123 wrote:
>> > 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 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/>
 
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