#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char str[100]; char buf[10]; int i; int n = 0; bool flag = 0; int j = 0; //gets(str); scanf("%s", str); for (i = 0; i != strlen(str); ++ i) { if (str[i] >= '0' && str[i] <= '9') { buf[j] = str[i]; j++; flag = 1; } else if (flag) { n += atoi(buf); memset(buf,0,10);//清空缓存区 flag = 0; j = 0; } } printf("%s",buf); printf("%d\n", n); }
取出字符串中的数字求和(c语言)
最新推荐文章于 2022-05-28 19:40:14 发布