Trim function in C, to trim in place (without returning the string)
Posted
by user364100
on Stack Overflow
See other posts from Stack Overflow
or by user364100
Published on 2010-06-11T00:41:27Z
Indexed on
2010/06/11
0:53 UTC
Read the original article
Hit count: 345
I can't figure out what to do to make this work. Here's my code:
char* testStr = " trim this ";
char** pTestStr = &testStr;
trim(pTestStr);
int trim(char** pStr)
{
char* str = *pStr;
while(isspace(*str)) {
(*pStr)++;
str++;
}
if(*str == 0) {
return 0;
}
char *end = str + strlen(str) - 1;
while(end > str && isspace(*end))
end--;
*(end+1) = 0;
return 0;
}
I get an access violation on *(end+1) = 0;, but I can't declare my testStr[] as such to avoid that, because I can't pass the pointers that way. Any ideas?
© Stack Overflow or respective owner