写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数(C语言)
题目要求:如标题
以下是具体代码:
#include <stdio.h>int main(){void calculate_characters(char str[]);char string[1000];printf("please enter a string: ");gets(string);calculate_characters(string);return 0;}void calculate_characters(char str[]){int vocabulary=0,digit=0,space=0,other=0;int i;for(i=0;str[i]!='\0';i++){if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z'))vocabulary++;else if(str[i]>='0' && str[i]<='9')digit++;else if(str[i]==' ')space++;elseother++;}printf("vocabulary: %d\ndigit: %d\nspace: %d\nother: %d\n",vocabulary,digit,space,other);}