Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Trimming whitespaces

Reply
Thread Tools

Trimming whitespaces

 
 
john_g83@hotmail.com
Guest
Posts: n/a
 
      06-23-2005
have a bit of c code that is ment to take a string (that may or may not
have spaces before or after the string) i.e. " stuff ", and trims off
the whitespace before and after.
Code:

char *trim (char *str, char ch)
{
char *first, *last;
int count;

/* Move first to the first character that isn't the same as ch */
for (first = str; *first == ch; first++);
/* Move last to the null character. Thats the only way to know 100% we
are
* removing items from the end of the string */
for (last = first; *last != '\0'; last++);
/* Ok now we backtrack until we find a character that isn't the same as
ch */
for (last--; *last == ch; last--);

if ( first != str)
{
for (count=0; count< last - first + 1; count++)
str[count] = *(first+count);
str[count] = '\0';
}
else
{
str[last-first] = '\0';
}

return str;
}

the problem is that it always removes the last letter of str as well.
i.e.
" stuff " -> "stuf" any ideas why this is happening.
Cheers
John

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-23-2005
wrote:
>
> have a bit of c code that is ment to take a string
> (that may or may not
> have spaces before or after the string) i.e. " stuff ", and trims off
> the whitespace before and after.
> Code:
>
> char *trim (char *str, char ch)
> {
> char *first, *last;
> int count;
>
> /* Move first to the first character that isn't the same as ch */
> for (first = str; *first == ch; first++);


That line could over run an array when (ch == '\0').

> /* Ok now we backtrack until we find a character that
> isn't the same as ch */
> for (last--; *last == ch; last--);


When a string consists of a null terminated array of
characters which are all equal to ch, then what happens?

--
pete
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-23-2005
pete wrote:
>
> wrote:
> >
> > have a bit of c code that is ment to take a string
> > (that may or may not
> > have spaces before or after the string)
> > i.e. " stuff ", and trims off
> > the whitespace before and after.
> > Code:
> >
> > char *trim (char *str, char ch)
> > {
> > char *first, *last;
> > int count;
> >
> > /* Move first to the first character that isn't the same as ch */
> > for (first = str; *first == ch; first++);

>
> That line could over run an array when (ch == '\0').
>
> > /* Ok now we backtrack until we find a character that
> > isn't the same as ch */
> > for (last--; *last == ch; last--);

>
> When a string consists of a null terminated array of
> characters which are all equal to ch, then what happens?


#include <string.h>

char *trim(char *str, char ch)
{
char *const p = str;

while (*str != '\0' && *str == ch) {
++str;
}
memmove(p, str, 1 + strlen(str));
str = p + strlen(p);
while (str != p && *--str == ch) {
*str = '\0';
}
return p;
}

--
pete
 
Reply With Quote
 
Rajan
Guest
Posts: n/a
 
      06-23-2005
John,
Are you passing const char* as an argument to the trim function?
i.e. let's say in main() are you invoking trim(" stuff ", ' ');
If you are passing const char* like this you can't do any changes in
str[] subscript because this is a read-only section which you can't
change.
If you have to pass an argument in trim , it has to be either an array
address or allocated pointer.

 
Reply With Quote
 
Rajan
Guest
Posts: n/a
 
      06-23-2005
John,
This is a code which will work fine:-

char *trim (char *str, char ch)
{
char *first, *last;

for (first = str; *first == ch; first++);
str = first;
for (last = str; *last != ch; last++) ;
*last = '\0';
return str;
}

 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      06-23-2005


Rajan wrote:
> John,
> This is a code which will work fine:-
>
> char *trim (char *str, char ch)
> {
> char *first, *last;
>
> for (first = str; *first == ch; first++);
> str = first;
> for (last = str; *last != ch; last++) ;
> *last = '\0';
> return str;
> }
>


It would not work if str is a empty string, i.e. "".
Also, it will fail if all the characters in str are value ch, i.e.
char buf[32] = "aaaaaaa";
trim(buf,'a');

--
Al Bowers
Tampa, Fl USA
mailto: (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-23-2005
On Thu, 23 Jun 2005 09:24:53 -0400, Al Bowers wrote:

> Rajan wrote:
>> John,
>> This is a code which will work fine:-
>>
>> char *trim (char *str, char ch)
>> {
>> char *first, *last;
>>
>> for (first = str; *first == ch; first++);
>> str = first;
>> for (last = str; *last != ch; last++) ;
>> *last = '\0';
>> return str;
>> }
>>

>
> It would not work if str is a empty string, i.e. "".
> Also, it will fail if all the characters in str are value ch, i.e.
> char buf[32] = "aaaaaaa";
> trim(buf,'a');


.... and it finds the first "ch" after first "non-ch" not the first of a
conscutive run of "ch" at the end of str as - the original code clearly
intended.

Because this function returns a pointer other than the one it was passed
if the storage is malloced it can't be freed with out holding onto the
original pointer somewhere else making it complicated to use in some
situations.

--
Ben.

 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      06-23-2005


Ben Bacarisse wrote:

> On Thu, 23 Jun 2005 09:24:53 -0400, Al Bowers wrote:
>
>
>>Rajan wrote:
>>
>>>John,
>>>This is a code which will work fine:-
>>>
>>>char *trim (char *str, char ch)
>>>{
>>>char *first, *last;
>>>
>>>for (first = str; *first == ch; first++);
>>>str = first;
>>>for (last = str; *last != ch; last++) ;
>>>*last = '\0';
>>>return str;
>>>}
>>>

>>
>>It would not work if str is a empty string, i.e. "".
>>Also, it will fail if all the characters in str are value ch, i.e.
>>char buf[32] = "aaaaaaa";
>>trim(buf,'a');

>
>
> ... and it finds the first "ch" after first "non-ch" not the first of a
> conscutive run of "ch" at the end of str as - the original code clearly
> intended.
>
> Because this function returns a pointer other than the one it was passed
> if the storage is malloced it can't be freed with out holding onto the
> original pointer somewhere else making it complicated to use in some
> situations.
>

Somewhat similiar to the "complicated" use of function realloc.
Example:
buf = realloc(buf, size)
intead of
char *tmp = realloc(buf,size)

More troublesome to me is that function trim as defined above, must
have synopsis saying to not use the function if the str is an
empty string, or if str consists entirely of characters ch.


--
Al Bowers
Tampa, Fl USA
mailto: (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
john_bode@my-deja.com
Guest
Posts: n/a
 
      06-23-2005


wrote:

[snip code]

> the problem is that it always removes the last letter of str as well.
> i.e.
> " stuff " -> "stuf" any ideas why this is happening.
> Cheers
> John


Huh. I'm not getting that result based on the same test data (" stuff
"). You sure that's the code you're actually running?

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-23-2005
wrote:
>
> have a bit of c code that is ment to take a string (that may or
> may not have spaces before or after the string) i.e. " stuff ",
> and trims off the whitespace before and after.
> Code:
>
> char *trim (char *str, char ch)


Untested:

char *trim(char *s, char ch)
{
char *p;

if (s && *s && ch) { /* avoid evil cases */
while (ch == *s) s++; /* trims leading. */
p = s; /* must be advanced over entry */
while (*p) p++; /* find end of string */
p-- /* last char in string */
while ((p > s) && (ch == *p)) p--;
*p = '\0';
}
return s; /* ok in evil cases */
}

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
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
open file with whitespaces mardif Python 6 05-20-2006 10:28 AM
Setting a schema path that contains whitespaces (Xerces) Prawdziwa Blondynka XML 0 03-07-2005 06:56 PM
Preserving whitespaces in dom4j. Kimos Java 0 04-14-2004 01:53 AM
Re: XML: How to remove whitespaces from element's text? Joona I Palaste Java 1 01-07-2004 06:44 PM
XML: How to remove whitespaces from element's text? Berco Java 0 01-07-2004 06:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57