R
rkk
Hi,
I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:
char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */
/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}
It is failing in the line having the code *(end+1) = '\0';. I think it
may be due to portablility issues & I need this code to work on all
well known platforms & compilers. Any sugesstions/help please?
Thank you.
Regards
Kalyan
I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:
char *trim(char *s)
{
char *begin,*end;
begin = s;
/* ltrim start */
while(*begin != '\0') {
if(isspace(*begin)) {
begin++;
} else {
s = begin;
break;
}
}
/* ltrim end */
/* rtrim start */
end = s + strlen(s) -1;
while( end !=s && isspace(*end) ) {
end--;
}
printf("%c\n",*end);
*(end+1) = '\0';
while(*s && isspace(*s)) s++;
/* rtrim end */
return s;
}
It is failing in the line having the code *(end+1) = '\0';. I think it
may be due to portablility issues & I need this code to work on all
well known platforms & compilers. Any sugesstions/help please?
Thank you.
Regards
Kalyan