- UID
- 10919
注册时间2006-4-18
阅读权限10
最后登录1970-1-1
周游历练
该用户从未签到
|
来这里混一贴,/: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[255];
-
- while (str[nStrLen++] != '\0');
-
- for (; i < nStrLen; i++) {
- if ((str[i] >= 0x30) && (str[i] <= 0x39)) {
- szGettedNumber[nNumberLen++] = str[i];
- continue;
- }else{
- if (!nNumberLen) continue;
- szGettedNumber[nNumberLen] = '\0';
- uSum += atoi(szGettedNumber);
- nNumberLen = 0;
- }
- }
-
- printf("the digit\'s sum within string %s is %d\n", str, uSum);
- }
复制代码 |
|