我刚接触C语言,正在学习C90.
我的目标是解析如下字符串:
NA ME, NAME , 123 456, 124 , 14134, 134. 134 , 1
对此:
NA ME,NAME,123 456,124,14134,134. 134,1
因此,参数中的白色字符仍然存在,但其他白色字符被移除.
我考虑过使用strtok,但我仍然想保留逗号,即使有多个连续的逗号.
到目前为止,我一直使用:
void removeWhiteChars(char *s)
{
int i = 0;
int count = 0;
int inNum = 0;
while (s[i])
{
if (isdigit(s[i]))
{
inNum = 1;
}
if (s[i] == ',')
{
inNum = 0;
}
if (!isspace(s[i]) && !inNum)
s[count++] = s[i];
else if (inNum)
{
s[count++] = s[i];
}
++i;
}
s[count] = '\0'; /* adding NULL-terminate to the string */
}
但它只跳过数字,直到逗号,才删除数字后面的白色字符,这是完全错误的.
我很感激任何帮助,我已经被困在这件事上两天了.