|
|||
|
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(); |
|
|
||||
|
||||
|
|
|
|||
|
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. |
|
|||
|
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; } |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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). |
|
|||
|
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 |
|
|||
|
"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; } |
|
|||
|
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; } |
|
|||
|
"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 |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|