c语言 输入一行字符(以$结束),统计其中的数字字符、空格字符出现的次数。求各位大神给个答案

2025-01-18 17:56:31
推荐回答(2个)
回答1:

#include

int main(void)

{

    char s;

int k=0,g=0;

while((s=getchar())!='$')

{

if(s==' ')

k++;

else if(s>='0'&&s<='9')

g++;

}

printf("the space have %d\tthe number have %d\n",k,g);

}

回答2:

#include 
#include
#include
#define N 80
int main(void)
{
    char s[N] = {0}, *p = s;
    int spaceNum = 0, digitNum = 0;
    fgets(s, N, stdin);
    s[N - 1] = '\0';
    while('\0' != *p && '$' != *p)
    {
        if (isdigit(*p))
        {
            ++digitNum;
        }
        else if (isspace(*p))
        {
            ++spaceNum;
        }
        else
        {
            ;
        }
        ++p;
    }
    printf("spaceNum = %d, digitNum = %d\n", spaceNum, digitNum);
    return 0;
}