简体   繁体   English

在 C 中计算大小写字母的问题

[英]Issue with counting upper and lower letters in C

I've written a program which should count upper and lower letters and other signs but it counts anything but when I click Enter and then ^C (EOF).我编写了一个程序,它应该计算大小写字母和其他符号,但是除了单击 Enter 和 ^C (EOF) 之外,它计算任何东西。 I don't know how to jump over it, hope somebody can help me somehow <3我不知道如何跳过它,希望有人能以某种方式帮助我<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 sends a SIGINT which normally just terminates your application. Ctrl + C发送一个SIGINT ,通常只会终止您的应用程序。

What you need is Ctrl + D , which triggers EOF.您需要的是Ctrl + D ,它会触发 EOF。

EDIT: Note that on Windows' default shell you may need Enter , Ctrl + Z , Enter (or F6 ) instead (though Ctrl + Z does something else entirely in Linux shells, sending a SIGSTOP ).编辑:请注意,在 Windows 的默认 shell 上,您可能需要EnterCtrl + ZEnter (或F6 )(尽管Ctrl + Z完全在 Linux 外壳中执行其他操作,发送SIGSTOP )。 See this question .看到这个问题

You could also compare against 0xD instead of EOF to catch Enter , or maybe use 0x1B which will catch Esc .您还可以与0xD而不是EOF进行比较来捕获Enter ,或者使用0x1B来捕获Esc This way you avoid the weirdness of how to trigger the end-of-input on different platforms (unless you want to process an input stream).这样您就可以避免在不同平台上触发输入结束的奇怪现象(除非您想处理输入流)。

Also take a look at this comment above as well as this answer which contain important additional info that I was missing!另请查看上面的此评论以及此答案,其中包含我缺少的重要附加信息!

CherryDt has already provided an appropriate answer. CherryDt 已经提供了适当的答案。


But just to add to that, EOF is not a character but instead an end condition .但补充一点, EOF 不是一个字符,而是一个结束条件 It may be OS dependent.它可能取决于操作系统。 You can't rely on it to work the same manner in every environment.您不能依靠它在每个环境中以相同的方式工作。 My suggest would be to use any character itself as a condition to terminate loop, rather than an condition which is environment dependent.我的建议是使用任何字符本身作为终止循环的条件,而不是依赖于环境的条件。

NOTE : The solution worked of ending the program with keys worked for me on Windows only when I included a fflush(stdin);注意:只有当我包含fflush fflush(stdin); after the getchar() .getchar()之后。 Probably, getchar() takes the input you give and leaves newline character \n in the input stream which was causing problem when I tried to terminate using ctrl + D or ctrl + Z or F6 .可能, getchar()接受您提供的输入并在输入 stream 中留下换行符\n ,当我尝试使用ctrl + Dctrl + ZF6终止时,这会导致问题。

But once you include fflush(stdin) , this would solve the problem and now the program ends successfully when I use the F6 on Windows.但是一旦你包含fflush(stdin) ,这将解决问题,现在当我在 Windows 上使用F6时程序成功结束。 You can also try with above mentioned keys if this does not work for you.如果这对您不起作用,您也可以尝试使用上述密钥。

Hope this helps some Windows users if the above answer wasn't working for them.如果上述答案对他们不起作用,希望这可以帮助一些 Windows 用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM