我有一串字母和数字,我必须找到其中从 0 到 9 的每个数字出现的频率。
我采用了嵌套的 for 循环,第一个用于数字,第二个用于字符串。然后使用 if.. else.. 来比较两者
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i,j,count=0;
char s[1000];
scanf("%s",s);
for(i=0;i<=9;i++)
{
for(j=0;j<strlen(s);j++)
{
if(i==s[j])
{
count=count+1;
}
}
printf("%d ",count);
count=0;
}
return 0;
}
如果输入为“abdf2343”,则输出必须为 0 0 1 2 1 0 0 0 0 0。 但我的输出是 0 0 0 0 0 0 0 0 0 0
最佳答案
请尝试此代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[1000];
scanf("%s",&s);
char str[10]={'0','1','2','3','4','5','6','7','8','9'};
int count=0;
for(int i=0;i<=9;i++)
{
for(int j=0;j<strlen(s);j++)
{
if(str[i]==s[j])
{
count=count+1;
}
}
printf("%d ",count);
count=0;
}
return 0;
}
关于c - 我有一串字母和数字,我必须找到其中每个数字从 0 到 9 的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57320922/