1. 程式人生 > >輸入一行字串,要求統計其中字母,空格,數字以及其他字元的數目

輸入一行字串,要求統計其中字母,空格,數字以及其他字元的數目

#include <stdio.h>
void main()
{
 int letter, space, digit, other;
 char ch;
 letter = space = digit = other = 0;
 while ((ch = getchar()) != '\n')
 {
  if (ch >= 'a' && ch <= 'z' || ch >= 'A'&&ch <= 'Z')
   letter++;
  else if (ch >= '0' && ch <= '9')
   digit++;
  else if (ch == ' ')
   space++;
  else
   other++;
 }
 printf("字母:%d\n", letter);
 printf("空格:%d\n", space);
 printf("數字:%d\n", digit);
 printf("其它字元:%d\n", other);
 getchar();
}

 

在這裡插入圖片描述