C程序。上部或下部...位掩码和while循环

时间:2014-09-16 11:00:19

标签: c

我试图编写一个让用户输入尽可能多的程序 他们想要的人物。然后程序将检查字符是大写还是小写。如果程序在输入流中检测到小写字母,程序将打印出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;
}

1 个答案:

答案 0 :(得分: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;
}