题目如图:
这里不再赘述
代码:
//字符串中统计与查询 //杨鑫 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 1000 char Str[MAXN]; /* *寻找字符串中最大的整数 * */ int maxnum_string(char str[]) { int i = 0, n = 0, maxNum = 0; while(str[i] != ‘\0‘) { if(str[i] >= ‘0‘ && str[i] <= ‘9‘) n = n * 10 + str[i] - ‘0‘; else { if(maxNum < n) maxNum = n; n = 0; } i++; } if(maxNum < n) maxNum = n; return maxNum; } /* *功能:统计字符串中的数字 * */ int count_number_string(char str[]) { int i = 0, count = 0; while(str[i] != ‘\0‘) { if(str[i] >= ‘0‘ && str[i] <= ‘9‘) { if(str[i+1] < ‘0‘ || str[i+1] > ‘9‘) { count++; } } i++; } return count; } int main() { int i = 0, count_main = 0, max = 0; printf("请输入一个字符串: "); gets(Str); printf("字符串的内容: "); puts(Str); count_main = count_number_string(Str); printf("字符串一共同拥有:%d个数字,", count_main); max = maxnum_string(Str); printf("最大数字为:%d\n", max); return 0; }
结果:
时间: 2024-10-11 06:14:56