C语言编程-7

    xiaoxiao2025-04-15  30

    编写一个程序,当输入一个字符串后,要求不仅能够统计其中字符的个数,还能分别指出其中大、小写字母、数字以及其他字符的个数。

    程序运行结果如下:

    输入:

    I am 21 years old.

    输出(五个数值依次为大、小写字母、数字、其他字符和总共含有的字符个数):

    1 10 2 5 18

    I enjoyed reading these books very much 1 32 0 6 39 #include <stdio.h> int main() { int u, l, n, others, sum; int i; char s[100] = ""; gets(s); u = 0; l = 0; n = 0; others = 0; sum = 0; i = 0; while(s[i] != '\0') { if(s[i]>='A' && s[i]<='Z') { u++; } else if(s[i]>='a' && s[i]<='z') { l++; } else if (s[i] > '0' && s[i] < '9') { n++; } else { others++; } sum++; i++; } printf("%d %d %d %d %d", u, l, n, others, sum); return 0; }

     

    最新回复(0)