#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define BOOL int
#define init_state 1
#define space_state 2
#define word_state 3
#define number_state 4
int main(void)
{
// BOOL init_state=1,space_state=2,word_state=3;
BOOL state=init_state;
int wordnum=0;//统计单词的个数
int spacenum=0;
int characnum=0;
int numbernum=0;
char *str=(char *)malloc(sizeof(char)*100);
char *strcopy;
printf("enter the string str:\n");
gets(str);
strcopy=str;
while(*str != '\0') //A~Z --> a~z
{
if(*str >='A' && *str <='Z')
{
*str=*str+'a'-'A';
}
str++;
}
str=strcopy;
while(*str !='\0')
{
switch(state)
{
case init_state:
{
if(*str == ' ')
{
state=space_state;
spacenum++;
}
else if(*str >='a' && *str<='z')
{
state=word_state;
wordnum++;
characnum++;
}
else
{
state=number_state;
numbernum++;
}
break;
}
case space_state:
{
if(*str >='a' && *str <='z')
{
state=word_state;
wordnum++;
characnum++;
}
else if(*str >='0' && *str<='9')
{
state=number_state;
numbernum++;
}
else
spacenum++;
break;
}
case word_state:
{
if(*str ==' ')
{
state=space_state;
spacenum++;
}
else if(*str >='0' && *str <= '9')
{
state=number_state;
numbernum++;
}
else
{
characnum++;
}
break;
}
case number_state:
{
if(*str ==' ')
{
state=space_state;
spacenum++;
}
else if(*str >='a' && *str<='z')
{
state=word_state;
wordnum++;
characnum++;
}
else
{
numbernum++;
}
break;
}
}
str++;
}
printf("the word' number in this string is %d\n",wordnum);
printf("the character' number in this string is %d\n",characnum);
printf("the space' number in this string is %d\n",spacenum);
printf("the number' number in this string is %d\n",numbernum);
return 0;
}