我看不出我的逻辑在代码中存在缺陷,出于某种原因,即使 ch 不等于 32,它也会向 i
加 1。
int main(void)
{
int ch;
float i, j;
printf("Enter a sentence: ");
for (i = 1, j = 0;;)
{
ch = getchar();
if (ch == '\n' || ch == EOF)
break;
if (ch != 32)
j++;
if (ch = 32)
i++;
}
printf("Average word length: %.2f", j/i);
return 0;
}
最佳答案
if (ch = 32)
存在问题,因为您将 ch 设置为 32。您应该使用等于运算符 if (ch == 32)
类似的问题也已得到解答here.
关于c - 平均字长 [C],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28993029/