Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.c

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-10-2012, 05:17 PM
peter
Guest
 
Posts: n/a
Default Right trim string function in C

Hello C programmers,
I was wondering does anybody knows how or is there
a right trim string function available in C?

Eg. Suppose I have the following:

char *str = "Hello Dolly \0";

Is there a right trim function that will remove the
trailing spaces and make *str look like "Hello Dolly\0"?

In PYTHON there is a useful function for this: str.rstrip();
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 02-10-2012, 05:36 PM
James Kuyper
Guest
 
Posts: n/a
Default Re: Right trim string function in C

On 02/10/2012 01:17 PM, peter wrote:
> Hello C programmers,
> I was wondering does anybody knows how or is there
> a right trim string function available in C?
>
> Eg. Suppose I have the following:
>
> char *str = "Hello Dolly \0";
>
> Is there a right trim function that will remove the
> trailing spaces and make *str look like "Hello Dolly\0"?


Sorry, there's no standard library function that does this. If it's
something you need a lot, you should write your own routine - it won't
be hard.

Reply With Quote
  #3 (permalink)  
Old 02-10-2012, 05:59 PM
Stefan Ram
Guest
 
Posts: n/a
Default Re: Right trim string function in C

peter <pc722@nospam.mail.com.br> writes:
>char *str = "Hello Dolly \0";

(...)
>In PYTHON there is a useful function for this: str.rstrip();


Untested and assuming that the string is in a mutable buffer:

#include <ctype.h>
#include <string.h>

void rstrip( char * const string )
{ char * last = string + strlen( string )- 1;
while( last >= string && isspace( last ))*last-- = 0; }

Reply With Quote
  #4 (permalink)  
Old 02-10-2012, 06:00 PM
Devil with the China Blue Dress
Guest
 
Posts: n/a
Default Re: Right trim string function in C

In article <4F3563A4.10302@verizon.net>, James Kuyper <jameskuyper@verizon.net>
wrote:

> On 02/10/2012 01:17 PM, peter wrote:
> > Hello C programmers,
> > I was wondering does anybody knows how or is there
> > a right trim string function available in C?
> >
> > Eg. Suppose I have the following:
> >
> > char *str = "Hello Dolly \0";
> >
> > Is there a right trim function that will remove the
> > trailing spaces and make *str look like "Hello Dolly\0"?

>
> Sorry, there's no standard library function that does this. If it's
> something you need a lot, you should write your own routine - it won't
> be hard.


Which is basically something like
n := strlen(s)
while n>0 and isspace(s[n-1]),
s[--n] := 0

However an important gotcha is the string has to be writable and all clients of
the string will see the trimming. If you may want to make a copy of the string
and modify that.

--
My name Indigo Montoya. | It's Coraline, not Caroline.
You flamed my father. | I'm whoever you want me to be.
Prepare to be spanked. | Annoying Usenet one post at a time.
Stop posting that! | At least I can stay in character.
Reply With Quote
  #5 (permalink)  
Old 02-10-2012, 06:30 PM
Barry Schwarz
Guest
 
Posts: n/a
Default Re: Right trim string function in C

On 10 Feb 2012 18:59:36 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
wrote:

>peter <pc722@nospam.mail.com.br> writes:
>>char *str = "Hello Dolly \0";

>(...)
>>In PYTHON there is a useful function for this: str.rstrip();

>
> Untested and assuming that the string is in a mutable buffer:
>
>#include <ctype.h>
>#include <string.h>
>
>void rstrip( char * const string )
>{ char * last = string + strlen( string )- 1;
> while( last >= string && isspace( last ))*last-- = 0; }


If the string consists entirely of blanks, is it undefined behavior
for last to end up pointing before the start of the string? This
value is not dereferenced but it is used one last time in the first
relational expression.

Why have all the solutions so far changed all the trailing blanks to
nuls? Wouldn't it be sufficient and more efficient to change only the
first one?

while( last > string && isspace( last-- ));
*++last = 0; }

which has the "advantage" of changing a string of all blanks to one
with a single blank instead of the empty string.

--
Remove del for email
Reply With Quote
  #6 (permalink)  
Old 02-10-2012, 06:45 PM
James Kuyper
Guest
 
Posts: n/a
Default Re: Right trim string function in C

On 02/10/2012 02:30 PM, Barry Schwarz wrote:
> On 10 Feb 2012 18:59:36 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
> wrote:
>
>> peter <pc722@nospam.mail.com.br> writes:

[snipage restored:]
>>> Is there a right trim function that will remove the
>>> trailing spaces and make *str look like "Hello Dolly\0"?

....
> Why have all the solutions so far changed all the trailing blanks to
> nuls? ...


Because that's what the OP requested (see above).

Reply With Quote
  #7 (permalink)  
Old 02-10-2012, 06:46 PM
Morris Keesan
Guest
 
Posts: n/a
Default Re: Right trim string function in C

On Fri, 10 Feb 2012 13:59:36 -0500, Stefan Ram <ram@zedat.fu-berlin.de>
wrote:

> peter <pc722@nospam.mail.com.br> writes:
>> char *str = "Hello Dolly \0";

> (...)
>> In PYTHON there is a useful function for this: str.rstrip();

>
> Untested and assuming that the string is in a mutable buffer:
>
> #include <ctype.h>
> #include <string.h>
>
> void rstrip( char * const string )
> { char * last = string + strlen( string )- 1;
> while( last >= string && isspace( last ))*last-- = 0; }


I believe that this invokes undefined behavior if strlen(string) == 0.

--
Morris Keesan -- mkeesan@post.harvard.edu
Reply With Quote
  #8 (permalink)  
Old 02-10-2012, 06:54 PM
Stefan Ram
Guest
 
Posts: n/a
Default Re: Right trim string function in C

"Morris Keesan" <mkeesan@post.harvard.edu> writes:
>>#include <ctype.h>
>>#include <string.h>
>>
>>void rstrip( char * const string )
>>{ char * last = string + strlen( string )- 1;
>> while( last >= string && isspace( last ))*last-- = 0; }

>I believe that this invokes undefined behavior if strlen(string) == 0.


#include <ctype.h>
#include <string.h>

void rstrip( char * const string )
{ char * p = string + strlen( string );
while( p-- > string && isspace( p ))*p = 0; }

Reply With Quote
  #9 (permalink)  
Old 02-10-2012, 07:06 PM
Stefan Ram
Guest
 
Posts: n/a
Default Re: Right trim string function in C

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>void rstrip( char * const string )
>{ char * p = string + strlen( string );
> while( p-- > string && isspace( p ))*p = 0; }


or, more careful:

void rstrip( char * const string )
{ char * p = string + strlen( string ); if( p > string )
while( p-- > string && isspace( p ))*p = 0; }


Reply With Quote
  #10 (permalink)  
Old 02-10-2012, 07:39 PM
Scott Fluhrer
Guest
 
Posts: n/a
Default Re: Right trim string function in C


"peter" <pc722@nospam.mail.com.br> wrote in message
news:jh3n08$6hs$1@speranza.aioe.org...
> Hello C programmers,
> I was wondering does anybody knows how or is there
> a right trim string function available in C?
>
> Eg. Suppose I have the following:
>
> char *str = "Hello Dolly \0";
>
> Is there a right trim function that will remove the
> trailing spaces and make *str look like "Hello Dolly\0"?


Other people have given you advice about how to do it (it isn't difficult),
but they mentioned that the string must be in a "mutable buffer", without
spelling out what that is.

Here's the issue; when you use a constant string using the "string" context,
as in:

char *str = "Hello Dolly \0";

that string lies in memory that you're not supposed to modify. If you do
modify it, well, C doesn't say what happens; the compiler can do whatever it
thinks is appropriate (which might not what you think is appropriate). In
practice, common behaviors could be:
- The program terminates (yes, really)
- The string is not changed, but the program continues anyways
- The string gets modified as you would expect
- The string gets modified, and in addition, other strings throughout the
program also get modified

If you have such a string, and you want to modify it, you'll need to copy
it, and modify the copy.

The exception to the above rule is if you declare str to be an array of
char's, as:

char str[] = "Hello Dolly \0";

If you do that, you can modify str to your hearts content (as long as you
don't write past the end of the array)...


Now, if you're doing this on nonfixed strings (say, strings you read from a
file), well, this isn't an issue, or as a wise woman once said: "Never Mind"

--
poncho


Reply With Quote
  #11 (permalink)  
Old 02-10-2012, 08:56 PM
Ben Bacarisse
Guest
 
Posts: n/a
Default Re: Right trim string function in C

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>void rstrip( char * const string )
>>{ char * p = string + strlen( string );
>> while( p-- > string && isspace( p ))*p = 0; }


Both here and below you meant to write isspace(*p).

> or, more careful:
>
> void rstrip( char * const string )
> { char * p = string + strlen( string ); if( p > string )
> while( p-- > string && isspace( p ))*p = 0; }


No, that has a similar problem. Unfortunately you've cut the context so
it won't be clear what you were correcting. The problem was
constructing an invalid pointer that points before the start of the
string and this code can also do that when the string is all spaces.

In addition to being careful about the pointers, you need to finesse the
mess that is isspace (and friends) when char might be signed. It's a
shame that what should be a simple function is really quite tricky.

char *rstrip(unsigned char *string)
{
char *ep = strchr(0);
while (ep > string && isspace(ep[-1])) --ep;
*ep = 0;
return string;
}

(The unsigned char * just is to avoid cluttering the code with a cast or
Tim's exotic compound literal union.)

--
Ben.
Reply With Quote
  #12 (permalink)  
Old 02-10-2012, 09:08 PM
James Kuyper
Guest
 
Posts: n/a
Default Re: Right trim string function in C

On 02/10/2012 04:56 PM, Ben Bacarisse wrote:
....
> char *rstrip(unsigned char *string)
> {
> char *ep = strchr(0);


strchr(string, 0)?

Reply With Quote
  #13 (permalink)  
Old 02-10-2012, 09:15 PM
Ben Bacarisse
Guest
 
Posts: n/a
Default Re: Right trim string function in C

James Kuyper <jameskuyper@verizon.net> writes:

> On 02/10/2012 02:30 PM, Barry Schwarz wrote:
>> On 10 Feb 2012 18:59:36 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>> wrote:
>>
>>> peter <pc722@nospam.mail.com.br> writes:

> [snipage restored:]
>>>> Is there a right trim function that will remove the
>>>> trailing spaces and make *str look like "Hello Dolly\0"?

> ...
>> Why have all the solutions so far changed all the trailing blanks to
>> nuls? ...

>
> Because that's what the OP requested (see above).


Only one blank needs to be changed to a null to satisfy the OP's
request. So far, the solutions have done more than that. It's not
wrong, but it is noteworthy.

--
Ben.
Reply With Quote
  #14 (permalink)  
Old 02-10-2012, 09:17 PM
pete
Guest
 
Posts: n/a
Default Re: Right trim string function in C

James Kuyper wrote:
>
> On 02/10/2012 02:30 PM, Barry Schwarz wrote:
> > On 10 Feb 2012 18:59:36 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
> > wrote:
> >
> >> peter <pc722@nospam.mail.com.br> writes:

> [snipage restored:]
> >>> Is there a right trim function that will remove the
> >>> trailing spaces and make *str look like "Hello Dolly\0"?

> ...
> > Why have all the solutions so far changed all the trailing blanks to
> > nuls? ...

>
> Because that's what the OP requested (see above).


I interpreted it the way that Barry Scharwz did:
that the string is the thing
from which the trailing spaces should be removed.

--
pete
Reply With Quote
  #15 (permalink)  
Old 02-10-2012, 09:21 PM
James Kuyper
Guest
 
Posts: n/a
Default Re: Right trim string function in C

On 02/10/2012 05:15 PM, Ben Bacarisse wrote:
> James Kuyper <jameskuyper@verizon.net> writes:
>
>> On 02/10/2012 02:30 PM, Barry Schwarz wrote:
>>> On 10 Feb 2012 18:59:36 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>>> wrote:
>>>
>>>> peter <pc722@nospam.mail.com.br> writes:

>> [snipage restored:]
>>>>> Is there a right trim function that will remove the
>>>>> trailing spaces and make *str look like "Hello Dolly\0"?

>> ...
>>> Why have all the solutions so far changed all the trailing blanks to
>>> nuls? ...

>>
>> Because that's what the OP requested (see above).

>
> Only one blank needs to be changed to a null to satisfy the OP's
> request. So far, the solutions have done more than that. It's not
> wrong, but it is noteworthy.


He asked to have the trailing blanks removed. That's "blanks", plural -
removing only a single blank doesn't do the job. It's possible that what
you're suggesting is what he actually meant, but I've worked in
situations where it was important to zero-out the unused portion of a
buffer containing a string, and that's what I assumed he was looking
for. Several others seem to agree.



Reply With Quote
 
Reply

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 05:32 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.