【发布时间】:2021-12-27 19:35:18
【问题描述】:
此代码不起作用,数字 0 到 9 在给定字符串中重复了多少次。
这是问题,来自hackerrank:
给定一个由字母和数字组成的字符串 S,求给定字符串中每个数字出现的频率。
我在我的逻辑中找不到任何错误。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
char s[1000];
int count[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
char temp;
scanf("%s", s);
for (int i = 0; i < 10; i++)
{
temp = i;
for (int j = 0; j < strlen(s); j++)
{
if (s[j] == i)
{
count[i] = count[i] + 1;
continue;
}
else
{
continue;
}
}
}
for (int k = 0; k < 10; k++)
{
printf("%d ", count[k]);
}
return 0;
}
【问题讨论】:
-
您是否尝试过使用调试器跟踪执行,直到它的行为与您预期的不同?
标签: c for-loop if-statement c-strings digits