我有一串字母和数字,我必须在其中找到每个数字从0到9的频率。
我将嵌套用于循环,第一个用于数字,第二个用于字符串。然后将两者与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 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;
}