strstrip

g_strstrip( string ) 函式來去除字元串的前後空白,結果發現一直出段錯誤。 原來是用來測試的字元串初始化不正確導致的。 懷疑直接使用注釋中的代碼,得到的字元串不是以NULL結尾,所以導致這個問題。

C標準庫沒有提供,但經常用到一個字元串相關函式
原型:char *strstrip(char *s)
參數:s是一個字元串
返回值:返回一個字元串
功能:去掉字元串s中的前後空白符(制表符、空格、回車等),並將該字元串返回。
一種實現:
char *strstrip(char *s)
{
size_t size;
char *end;
size = strlen(s);
if (!size)
return s;
end = s + size - 1;
while (end >= s && isspace(*end))
end--;
*(end + 1) = '\0';
while (*s && isspace(*s))
s++;
return s;
}

相關詞條

熱門詞條

聯絡我們