单词有多少?
成绩: 0 / 折扣: 0.8
背景:
用空格或换行分开的字符串称为单词。输入多行字符串,直到遇到了单词 "stop" 时才停止。最后输出单词的数量。
输入:
多个字符串
输出:
单词的数量
Simple input
china abc 123 bstop stop
Simple output
5
我这有一现成程序,自己改改。
#include"stdio.h"
int alphabet(const char c)
{
int i;
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
i=1;
else
i=0;
return i;
}
int countwords(const char string[])
{
int i,wordcount=0,lookingforword=1;
int alphabet(const char c);
for (i=0;string[i]!='\0';++i)
if(alphabet(string[i]))
{
if(lookingforword)
{
++wordcount;
lookingforword=0;
}
}
else
lookingforword=1;
return wordcount;
}
void readline(char buffer[])
{
char character;
int i=0;
do{
character=getchar();
buffer[i]=character;
++i;
}while (character!='\n');
buffer[i-1]='\0';
}
int main(void)
{
char text[81];
int countwords(const char string[]);
void readline(char buffer[]);
int endoftext=0,totalwords=0;
printf("Type in your text.\n");
printf("When you are done,press 'RETURN'.\n\n");
while(!endoftext)
{
readline(text);
if(text[0]=='\0')
endoftext=1;
else
totalwords+=countwords(text);
}
printf("There are %i in text.\n",totalwords);
getch();
return 0;
}全部是自己写的函数,没调用,长了点,将就看下。
[此贴子已经被作者于2007-6-26 23:25:31编辑过]