一个可以随意转抄的题
来这里混一贴,/:017有一个字符串,里面包含一些数字,写一个函数,把这些数字加起来。比如“我30你40他50”结果就是120.
#include <stdio.h>
#include <stdlib.h>
unsigned char str[] = "我30你40他50";
void main()
{
int nStrLen = 0, nNumberLen = 0, i = 0;
unsigned int uSum = 0;
char szGettedNumber;
while (str != '\0');
for (; i < nStrLen; i++) {
if ((str >= 0x30) && (str <= 0x39)) {
szGettedNumber = str;
continue;
}else{
if (!nNumberLen) continue;
szGettedNumber = '\0';
uSum += atoi(szGettedNumber);
nNumberLen = 0;
}
}
printf("the digit\'s sum within string %s is %d\n", str, uSum);
}
页:
[1]