长沙简牍博物馆导游词:C程序编程7

来源:百度文库 编辑:神马品牌网 时间:2024/05/14 14:22:30
编写函数int countw(char *str),统计字符串str中单词的个数.单词之间以空格, 逗号, 句号作分隔,数字也看做单词.单词之间可能不止一个分隔符.如输入:
It's 10:10 o'clock ,I am late.屏幕上输出"There are 6 words." */

int countw(char *str)
{}
main()
{char s[200];
gets(s);
printf("There are %d words.\n",countw(s));
}

int countw(char *str)
{int flag=0,n=0;
while(*str)
{if(*str==' '||*str==','||str=='.') flag=0;
else if(flag==0)
{flag=1;
n++;}
str++;}
return n;
}
main()
{char s[200];
gets(s);
printf("There are %d words.\n",countw(s));
}