问题描述:从字符串中提取连续的整数,并统计个数
编译环境:vc++6.0
代码:
#include <stdio.h>
/*从字符串中提取连续的整数,并统计个数*/
int main()
{
char str[81], * p = str;
int count = 0, num;
printf("请输入含数字的字符串:\n");
gets(str);
while (*p)
{
if (*p >= '0' && *p <= '9') //找到数字
{
for (num = 0; *p >= '0' && *p <= '9'; p++) //处理连续的数字字符
num = num * 10 + *p - '0';
count++;
printf("NO%d:%d\t", count, num);
}
else if (*p != '\0' && *p < '0' || *p>'9') //遇到非数字字符
p++; //跳过非数字字符
}
printf("\n共有%d个数\n",count);
return 0;
}
测试案例: