解决方法
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
using namespace std;
int main()
{
char str[256];
int n=0;
cin.getline(str,256);
int len =strlen(str);
for(int i=0;i<len;i++){
if(str[i]>='0'&&str[i]<='9')
n++;
}
cout<<n;
system("pause");
return 0;
}
gets(str)------>cin.getline(str,256);
该C++代码段从用户输入读取一行字符串,然后计算并输出其中数字字符的总数。使用getline函数替代了gets函数以避免缓冲区溢出问题,并通过遍历字符串进行数字检测。
1万+

被折叠的 条评论
为什么被折叠?



