trim() function in C?

quick question: I'm looking for a way to remove the leading spaces from a character array. I know I could right my own algorithm to do this, but I was just curious if there is a pre-defined function or something for this purpose. I know other languages have functions like trim() and ltrim() and rtrim() -- does C have something like this? If so, can you tell me what it is and what library I would have to include to use it? Thank you!

MacBook, Mac OS X (10.5.8)

Posted on Nov 8, 2009 3:36 AM

Reply
9 replies

Nov 8, 2009 5:43 AM in response to Tron55555

I don't think trim is part of the standard C runtime library, and in fact I think I wrote my own rtrim and ltrim for my C Windows programming library. When you start working with Cocoa you'll have access to a NSString method called stringByTrimmingCharactersInSet:. Until then I think writing your own would make for an excellent exercise, and result in a set of functions you could use in the future--a nice start on your own private library. Another really handy function that everyone needs to write for themselves is called "crunch". That not only trims leading and trailing white space, it reduces embedded white space runs to only one space char. This is a critical function when writing parsers that need to work with raw user-entered text. For example, if you're looking for "good morning", you don't want the parse to fail because the user has used 2 spaces between the words.

There are some functions that programmers like to write for themselves. Then you always know exactly what they do (eventually). So when someone who can't do low level programming needs to rely on a library "trim" that doesn't work for tabs or non-printing chars (which can mess up your parse in a hurry), you just smile and advise them to Google some more for a better library.

\- Ray

Nov 8, 2009 4:28 PM in response to RayNewbie

For parsing, I tend to use strtok(). I've rarely worried about trimming my lines.

p = strtok(str, " ");
while( p != NULL ) {
... do what I need with this white space separated substring ...
p = strtok(NULL, " "); /* get next substring */
}

The first call to strtok() passes the string. The following calls pass a NULL otherwise strtok() reinitializes.

The passed string is modified by strtok() so if you need the original, use a copy of the string.

The separating characters can be anything desired. You can change the separating characters from one call to the next if desired.

But with all things, there are exception situations, so there are also times I've written my own character-by-character scanner that does what I need for that specific job.

And I also work with shell scripts, awk, and Perl creating tools that aid in my work environment.

Nov 8, 2009 6:46 PM in response to BobHarris

Thanks, Bob! I certainly agree that strtok()--and more recently strsep()--should be in every programmer's bag of tricks, and I wouldn't be surprised if Tron's next thread follows up on your tip. Teaching Tron a new runtime function can be very rewarding, but the good deed carries with it some degree of responsibility. So I think that next thread will be all yours.

In my case, it's sometimes easier to crunch because I may be scanning certain patterns out of rich text, and when I find them I want to include them in a database. For example if I'm looking for the title of a section, I might seek to a particular paragraph style in RTF. If I recognize the title, I might prefer the crunched version in the database based on the assumption that excess white space is most likely a typo rather than a unique phrase that needs a new key (of course I might save the raw text as well so the document's author can receive a courteous e-mail pointing out that wrist protectors and sandwiches should be positioned away from the space bar). I used to get more interesting work btw, but lots of other pursuits were more interesting when I was younger.

So some of my nasty jobs are a little different from what we do in the front end of a compiler, etc. Whoever decided that a source code identifier couldn't include embedded spaces was really onto something. Where would we be today without that simple rule?

All the best,
\- Ray

Nov 10, 2009 5:16 AM in response to RayNewbie

I'm not sure if there will be a follow-up thread on that or not. I must be honest -- I'm not really sure what parsing is. It's certainly a word I'm familiar with, but I don't know exactly what it means. Could someone enlighten me?

Other than that, I'm just trying to strip the leading spaces off a string, so I think I'll write my own function for that. Like Ray said, I think it will be good practice for me.

Nov 10, 2009 6:16 AM in response to Tron55555

parse |pärs|
verb trans
analyze (a sentence) into its parts and describe their syntactic roles.
• Computing analyze (a string or text) into logical syntactic components, typically in order to test conformability to a logical grammar.
• examine or analyze minutely : he has always been quick to parse his own problems in public.
noun

Computing - an act of or the result obtained by parsing a string or a text.

ORIGIN mid 16th cent.: perhaps from Middle English pars (parts of speech,) from Old French pars ‘parts’ (influenced by Latin pars ‘part’ ).

Nov 11, 2009 3:20 AM in response to etresoft

Thanks, et. Yeah I checked out the definition of the word before I asked in the post, but I couldn't really apply what I read to programming. If anyone feels like it, I'm looking for an explanation or parsing as it applying to programming and whatnot. I know XML uses the term parsing a lot, but I don't even know what it means in that regard. It's not that important -- I'm just curious.

Nov 11, 2009 6:41 AM in response to Tron55555

How do you program? You use a programming language. A language has syntax and semantics. Parsing analyzes the syntax to identify language parts such as subject, verb, object, adjective, adverb - or identifier, parameter, type, operator, etc. Computer languages need to have very simple syntax and very unambiguous semantics so they can be parsed and compiled by a computer.

XML is very useful because it is a language with a very simple syntax, making it fast and easy to parse. Unlike other programming languages, it has infinitely complex semantics.

Nov 11, 2009 7:35 AM in response to etresoft

Okay -- that makes sense. I'm having trouble applying it to trimming leading/trailing spaces, but at least I get what it means now. Thanks for taking the time to educate me on that, et. It seems like such a basic term in programming yet my only understanding of it was in terms of English and grammar and whatnot, but I think I get it now. It's the same concept as with grammar, except instead of parsing sentences you are parsing programming statements -- breaking them down into their parts. Yeah, that makes sense. Thanks again.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

trim() function in C?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.