Comment on trim string function please

D

DiAvOl

My implementation of a trim function (It may contain bugs but it works
for me):

char *trim(char *str) {
int len;
while(isspace(*str))
str++;
len = strlen(str);

while(isspace(*(str+(--len))))
;

str[++len] = '\0';

return str;
}
 
I

Ian Collins

Richard said:
DiAvOl said:


Given an empty string, this function attempts to access the character that
comes *before* the start of the string. That's a showstopper.
Not to mention what happens with a NULL string...
 
B

Ben Bacarisse

Richard Heathfield said:
DiAvOl said:


You'll need <ctype.h> for this - and note that the behaviour is undefined
if a negative value is encountered before the first whitespace character
(on systems where char is signed).




Given an empty string, this function attempts to access the character that
comes *before* the start of the string. That's a showstopper.

Or any string consisting of only space characters (which includes, in
some sense, the empty string).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top