Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Right trim string function in C

Reply
Thread Tools

Right trim string function in C

 
 
peter
Guest
Posts: n/a
 
      02-10-2012
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
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      02-10-2012
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
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      02-10-2012
peter <(E-Mail Removed)> 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
 
Barry Schwarz
Guest
Posts: n/a
 
      02-10-2012
On 10 Feb 2012 18:59:36 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram)
wrote:

>peter <(E-Mail Removed)> 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
 
James Kuyper
Guest
Posts: n/a
 
      02-10-2012
On 02/10/2012 02:30 PM, Barry Schwarz wrote:
> On 10 Feb 2012 18:59:36 GMT, (E-Mail Removed)-berlin.de (Stefan Ram)
> wrote:
>
>> peter <(E-Mail Removed)> 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
 
Morris Keesan
Guest
Posts: n/a
 
      02-10-2012
On Fri, 10 Feb 2012 13:59:36 -0500, Stefan Ram <(E-Mail Removed)-berlin.de>
wrote:

> peter <(E-Mail Removed)> 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 -- http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      02-10-2012
"Morris Keesan" <(E-Mail Removed)> 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
 
Stefan Ram
Guest
Posts: n/a
 
      02-10-2012
(E-Mail Removed)-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
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-10-2012
(E-Mail Removed)-berlin.de (Stefan Ram) writes:

> (E-Mail Removed)-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
 
James Kuyper
Guest
Posts: n/a
 
      02-10-2012
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
 
 
 
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
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 2 04-24-2007 01:59 AM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 26 02-26-2007 05:06 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 6 12-25-2006 08:47 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 0 10-25-2006 11:00 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 0 08-28-2006 11:00 PM



Advertisments