Posted on 2007-07-05 15:04
停留的风 阅读(344)
评论(0) 编辑 收藏 所属分类:
C语言学习历程
#include <stdio.h>
int alphabetic(const char c)
{
int alp;
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
{
alp=0;
}
else alp=1;
return alp;
}
int countWords(const char string[])
{
int i,wordCount=0;
int lookingForWord=0;
for(i=0;string[i]!='\0';i++)
{
if(alphabetic(string[i])==0)
{
if(lookingForWord==0)
{
wordCount++;
lookingForWord=1;
}
}
else
{
lookingForWord=0;
}
}
return wordCount;
}
int main(void)
{
const char text1[]="Well, here goes!";
const char text2[]="And here we go...again.";
int countWords(const char string[]);
printf("%s-words=%i\n",text1,countWords(text1));
printf("%s-words=%i\n",text2,countWords(text2));
return 0;
}
运行图: