Hy,
How can I remove whitespaces from a string ? I.e. like trim() does in PHP. I'm in trouble with making my own.
Hy,
How can I remove whitespaces from a string ? I.e. like trim() does in PHP. I'm in trouble with making my own.
Post your code. Basically, set all the whitespace characters into a string of their own, and then use strstr() to find them. If a subsequent strstr() call returns an adjacent address to the prior saved address, you found a character you can remove.
If you work on your character array from right to left, it's easier.
Todd
Mainframe assembler programmer by trade. C coder when I can.
You need to make your own (or find one on the "interweb").
It's probably good practice to learn by making your own. The essential principle is that you need a read and a write position in the string, you then read a character. If it's not a space, write it. Continue this until you are at the end of the string. If you want to do exactly trim, then you will need to:
1. use "isspace()" to identify whitespace characters in the string.
2. Identify when you reach the first non-space character and stop removing spaces.
3. When you reach the end of the string, walk back across any whitespace and terminate the string after the point of non-space. [You can of course "remember the last non-space"].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
A nice do/while loop would do the trick
Code:do { if (*string != ' ') { *nospaces++ = *string; /*malloc nospaces */ i++; } } while(*string++);
That will remove ALL spaces, which is not what "trim" does, but yes, that works.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Sorry, read to quick. Just change the logic around...perhaps:
Code:if ((string[0] || string[strlen(string)]) == ' ') /* then assign your char array as needed */
Nice robust and portable solution, returns the start of the trimmed string. Should not be used on dynamically allocated strings, except if the pointer to the string trimmed start is stored at a different variable.Code:char* trim(char*s) { char *end = s + strlen(s)-1; while(*s && isspace(*s)) *s++ = 0; while(isspace(*end)) *end-- = 0; return s; }
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);
I have a theory: Programmers are such egomaniacs, you can post a homework question without any proof of effort, and eventually someone will give you the complete answer while trying to show off his expertise.
My best code is written with the delete key.
I am sorry but to not be misunderstood, firstly it didn't seem like a homework question, secondly even if it was, then the person doing the assignment might use a black box function to get him started, and thirdly the replies up to then were probably more confusing than a simple clean answer itself.
I don't recall any assignment that is about trimming strings, and i understand the purpose of the forum. But a familiar tool can always help someone to solve an unfamiliar problem.
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);
>firstly it didn't seem like a homework question
"Homework" is generally used here as a catchall term for stuff you should attempt to solve on your own before asking for a solution. More often than not it refers to tasks that any sane programmer would use an existing library to accomplish, yet for some reason the "homeworkee" wants to do manually.
>the replies up to then were probably more confusing than a simple clean answer itself
I don't disagree, but you'll also notice that the OP didn't ask for clarification.
>I don't recall any assignment that is about trimming strings
I've seen them before. In fact, I've seen a lot of assignments that involve string processing, and trim is one of the more basic ones.
My best code is written with the delete key.
I don't study IT at college (though I most probably plan to), so it's not a homework. I'm still in highschool and it's just a hobby of mine.
And..um..what kind of homework would be to remove whitespaces ? It that what it's like studying IT at school ? Solving this kind of puzzles ?
P.S. Thanks everyone for your help. My own (not sure actually if it's my own cause I read much of it from another forum) code was something like this:
Best wishes, DesCode:char *trim_spaces (char *str, int c) { if (str != NULL) { char ch = (char)c; char *a, *b; for (a = str; *a == ch; a++); str = a; for (b = str; *b != ch; b++) ; *b = '\0'; return str; } else return str; }
Last edited by Dave_Sinkula; 03-10-2008 at 08:54 AM. Reason: Tagged.
Homework can be all sorts of things, and it's obviously not always easy to determine what is homework or not. By the way, I didn't even suggest it was homework, I just didn't want to give you the code, because writing it yourself would be the best way to learn how to do things with strings.
Of course, finding the solution on another forum only teaches you better google skills, not programming skills.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.