| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1146 人关注过本帖
标题:[求助]有关单词计数的问题
只看楼主 加入收藏
神雕大侠
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-5-18
收藏
 问题点数:0 回复次数:3 
[求助]有关单词计数的问题

单词有多少?
成绩: 0 / 折扣: 0.8

背景:


用空格或换行分开的字符串称为单词。输入多行字符串,直到遇到了单词 "stop" 时才停止。最后输出单词的数量。


输入:


多个字符串


输出:


单词的数量

Simple input
china abc 123 bstop stop

Simple output
5


搜索更多相关主题的帖子: 单词 计数 stop 字符 Simple 
2007-06-26 22:35
huangfengchu
Rank: 1
等 级:新手上路
威 望:2
帖 子:274
专家分:0
注 册:2007-5-22
收藏
得分:0 

我这有一现成程序,自己改改。
#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编辑过]


深山苦学C语言,终年不见外面世界。
2007-06-26 22:41
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 
#include<stdio.h>
main(int i,char *a)
{
while(i++&&scanf("%s",a)&&strcmp(a,"stop"));
printf("%d",i-1);
}

羊肉串 葡萄干 哈密瓜!!
2007-06-26 23:18
神雕大侠
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-5-18
收藏
得分:0 
回复:(mp3aaa)#includemain(int i,...
while(i++&&scanf("%s",a)&&strcmp(a,"stop"));
上面这个语句是什么意思啊,调用strcmp函数的时候前面不是要加头文件#include<string.h> 的吗?这个没有加,怎么编译的时候没有报错啊?
2007-06-27 11:38
快速回复:[求助]有关单词计数的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011829 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved