C语言:输入一个字符串,编程统计其中的字母、数字、空格(含制表符)、标点符号的个数并输出。

提示:isalpha()判断字母函数、isdigit()判断数字函数、isspace()判断空格函数、ispunct()判断标点函数

#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
    char str[256];
    puts("请输入一个长度不超过200的字符串:");
    gets(str);
    int i = 0, alpha = 0, digit = 0, space = 0, spunct = 0;
    for(;str[i]!='\0';i++)
    {
        if(isalpha(str[i]))
            alpha++;
        else if(isdigit(str[i]))
            digit++;
        else if(isspace(str[i]))
            space++;
        else if(ispunct(str[i]))
            spunct++;
    }
    printf("该字符串的字母个数: %d个,数字个数: %d个,空格个数: %d个,标点个数: %d个\n",alpha,digit,space,spunct);
    return 0;
}

来自:求助得到的回答
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-12-11
#include<stdio.h>
#include<string.h>
int main()
{
char arr[200];
int i=0;
char a;
while(a=getchar() && a!='\n')
{
arr[i]=a;
i++;
}
int x=0,y=0,g=0,h=0;
for(int j=0;j<i;j++)
{
if(isalpha(arr[j]))
x++;

else if(isdigit(arr[j]))
y++;

else if(isspace(arr[j]))
g++;
else if(ispunct(arr[j]))
h++;
}
printf("字母有%d个,数字有%d个,空格有%d个,标点有%d个\n",x,y,g,h);
return 0;