【发布时间】:2020-04-28 19:54:00
【问题描述】:
我正在尝试通过用户输入来识别字母、数字和标点符号的数量,我得到了数字的数量,但字母和标点符号不正确!! .
我不知道为什么.. 这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char str[50];
int alphabet = 0 , number = 0, punct = 0;
printf("Enter your sentence: ");
fgets(str, sizeof(str),stdin);
for(int i=0 ; i<50; i++)
{
if(isalpha(str[i]) != 0)
{
alphabet++;
}
else if (isdigit(str[i]) != 0 ){
number++;
}
else if (ispunct(str[i]) != 0){
punct++;
}
}
printf("Your sentence contains:\n");
printf("Alphabets: %d",alphabet);
printf("\nDigits: %d",number);
printf("\nPunctuation: %d",punct);
return 0;
}
【问题讨论】:
-
顺便提一下,传递给
isalpha和其他分类函数的参数应该是unsigned char,而不是char。