《劍指 Offer》——41、和為S的連續正數序列
阿新 • • 發佈:2021-01-24
技術標籤:C語言練習
C語言入門
問題:
編寫一個程式,輸入一行字元,計算出這串字元中,字母,數字,空格,以及其他字元的個數;
答:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char c;
int letters=0, num=0, space=0, _other=0;
printf("請輸入一行字元\n");
while ((c = getchar()) != '\n')
{
if (c >= 'a'&&c <= 'z' || c >= 'A' &&c <= 'Z')
letters++;
else if (c == ' ')
space++;
else if (c >= '0'&&c <= '9')
num++;
else
_other++;
}
printf("letter=%d\nspace=%d\n_other=%d\nnum=%d\n", letters, space, _other,num);
system("pause");
}
輸入輸出例項