统计字符串中数字字母个数

img


刚学C,本题要求统计字母,数字,空格和其他字符个数,但统计结果不对,然而我看不出来哪地方问题,求解答,谢谢!

第一个if(),括号中's'应该去掉'',写成s就好
还有else是紧跟着上一个if的,即你最后的else表示的是非数字字符。

while里面第一个if,s不用引号,因为s是字符变量,加引号是字符s

img

解答如下

#include<stdio.h>
#include<string.h>
int main()
{
    char t[150];
    gets(t);
    int w=0,d=0,o=0,p=0;
    for(int j=0; j<strlen(t); j++)
    {
        if((t[j]<='Z'&&t[j]>='A')||(t[j]<='z'&&t[j]>='a'))
        {
            w++;
        }
        else if(t[j]<='9'&&t[j]>='0')
        {
            d++;
        }
        else if(t[j]==' ')
        {
        p++;
        }
        else
        {
            o++;
        }
    }
    printf("字母:%d\n数字:%d\n空格:%d\n其它:%d\n",w,d,p,o);
    return 0;
}

空格