我编写了一个程序,它应该计算大小写字母和其他符号,但是当我单击 Enter 然后单击 ^C (EOF) 时,它什么都算。我不知道如何跳过它,希望有人能以某种方式帮助我<3
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
int uppers = 0, lowers = 0, others = 0;
while((ch = getchar()) != EOF)
{
if(islower(ch))
lowers++;
else if(isupper(ch))
uppers++;
else
others++;
}
printf("\n\nUpper letters - %d Lower letters - %d Others- %d", uppers, lowers, others);
return 0;
}
最佳答案
Ctrl+C 发送一个 SIGINT
,通常只会终止您的应用程序。
你需要的是Ctrl+D,它会触发EOF。
编辑:请注意,在 Windows 的默认 shell 上,您可能需要 Enter、Ctrl+Z,Enter(或 F6)(尽管 Ctrl+Z 完全在 Linux shell 中执行其他操作,发送SIGSTOP
)。参见 this question .
您还可以与 0xD
而不是 EOF
进行比较以捕获 Enter,或者可能使用 0x1B
这将 catch Esc。通过这种方式,您可以避免在不同平台上触发输入结束的怪癖(除非您想处理输入流)。
另请查看 this comment以上以及this answer其中包含我遗漏的重要附加信息!
关于c - 在 C 中计算大小写字母的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62086431/