gpt4 book ai didi

c - 在C中显示字母数和小数位数

转载 作者:太空宇宙 更新时间:2023-11-04 07:11:35 24 4
gpt4 key购买 nike

我已经在这个简单的代码上工作了几个小时,我不知道哪里出了问题!我需要在标准输入中显示字母数和小数位数。到目前为止我有这个:

#include<stdio.h>
#include<ctype.h>
int isalpha(int);
int isdigit (int);
int main()
{
int c;
while((c=getchar())!=EOF)
printf("The number of letters is %d and the number of digits is %d.\n", isalpha(c), isdigit(c));
return 0;
}

int isalpha(int one)
{
int ch;
int i;
i=0;
scanf("%d", &ch);
if(isalpha(ch))
i++;
return i;
}

int isdigit(int two)
{
int a;
int k;
k=0;
scanf("%d", &a);
if(isdigit(a))
k++;
return k;
}

每当我尝试运行程序时,它就会崩溃,而且我不知道代码的哪一部分是错误的。虽然我在这个领域还没有太多经验,所以非常感谢任何帮助!先感谢您。

最佳答案

只需轻轻地使用现有的 API 并获得如下所示的计数

    int alp = 0;
int dig = 0;
while ((c = getchar()) != EOF)
{
if (isalpha(c)
alp++;
else if (isdigit(c))
dig++;
}

printf("The number of letters is %d and the number of digits is %d.\n", alp,dig);

PS:如果你在输入中有\n

,请注意刷新换行符

关于c - 在C中显示字母数和小数位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28012778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com