C语言 带逗号的字符串“123,456,789”转换数字123456789,怎么转换?

如题所述

从前向后获取每个字符,然后转换为数字,total = c
然后获取下一个字符转为数字,然后进行进阶加 total=total*10+c
如果是逗号,忽略,继续取下一个字符
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-10-08

写的不好,也不知道是否符合c语言的规范,按c++到是可以编译,看着用吧。。:

#include "math.h"
int StrToNum(const char * pstr)
{
int iresult = 0;
const char * pstrRun = pstr;
while (pstrRun[0] != '\0')
{
if (pstrRun[0] > 1 && pstrRun[0] <255 && isdigit(pstrRun[0]))
{
iresult *= 10;
iresult += (pstrRun[0] - '0');

}
++pstrRun;
}

return iresult;
}

本回答被提问者采纳
第2个回答  2015-04-20
输出是写%s以字符串的形式输出。
相似回答