我正在尝试编写一个程序,让用户输入尽可能多的
他们想要的字符。然后,程序将检查字符是大写还是小写。如果程序在输入流中检测到小写字母,则程序将打印出char类型及其十六进制数字的字母。当遇到EOF并且输入的所有字母均为大写字母时,程序将打印“所有输入的字母均为大写字母”,程序还将计数所有输入的字母,此数字也将显示。我真的是编程新手,请耐心等待。

这是我的代码

#include <stdio.h>

int main(void)
{
char myChar;

do
{
  myChar = getchar();
  printf("%02x ",myChar); // this is here to help debugging

  if(myChar == EOF) // if get user enters an EOF or the EOF is reached
     {
        printf("All are caps\n");
        // print  number of letters entered
        // break out of loop and end program
     }

}
while(((myChar&0x20) == 0));
{
      printf("\n");
      printf("entered LOWER CASE = %c \n",myChar);
      printf("The hex value is = %x \n",myChar); // hex value of lower case letter

}
system("pause");
return 0;
}

最佳答案

#include <stdio.h>
#include <ctype.h>

int main(void){
    int myChar;
    int allCaps = 1;
    int count = 0;

    while(EOF!=(myChar = getchar())){
        if(isupper(myChar)){
            ++count;
        } else {
            if(islower(myChar)){
                printf("entered LOWER CASE = %c\n", myChar);
                printf("The hex value is = %02x\n", myChar);
            } else if(myChar == '\n'){
                continue;
            }
            allCaps = 0;
        }
    }
    if(allCaps){
        printf("All letters entered are upper case\n");
        printf("number of letters entered : %d\n", count);
    }
    system("pause");
    return 0;
}

07-28 02:02
查看更多