統計出一字符串中英文字母、空格、數字的個數。
演示代碼:
void checkout(const char * str,int * alphabet,int * blank, int *figure)
{
if(str == NULL)
{
printf("str id NULL");
exit(-1);
}
char * tmp = str;
int Alphabet = 0;
int Blank = 0;
int Figure = 0;
while(*tmp != '\0')
{
if((*tmp >= 'a' && *tmp <= 'z') || (*tmp >= 'A' && *tmp <= 'Z'))
{
Alphabet++;
}
else if(*tmp == ' ')
{
Blank++;
}
else if(*tmp >= '0' && *tmp <= '9')
{
Figure++;
}
tmp++;
}
*alphabet = Alphabet;
*blank = Blank;
*figure = Figure;
}
int main()
{
char str[32] = { 0 };
printf("input str:");
gets(str);
int alphabet = 0;
int blank = 0;
int figure = 0;
checkout(str,&alphabet,&blank,&figure);
printf("alphabet has:%d\n",alphabet);
printf("blank has:%d\n",blank);
printf("figure has:%d\n",figure);
return 0;
}