社区
C语言
帖子详情
求出一篇英文文章中单词最长的单词
jxnucsb2008
2004-06-16 10:15:30
求出一篇英文文章中单词最长的单词
...全文
416
9
打赏
收藏
求出一篇英文文章中单词最长的单词
求出一篇英文文章中单词最长的单词
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
9 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
Jedimaster
2004-06-22
打赏
举报
回复
思路:
建立文件流
读取单词到一个临时变量
读取临时变量的长度并记录下来
继续读
如果比第一次大那么就保留否则抛弃读取下一个
直到EOF
martmy
2004-06-21
打赏
举报
回复
mark
liuweihug
2004-06-17
打赏
举报
回复
楼主,是不是要判断一下空格,特殊符号啊?
qingyuan18
2004-06-17
打赏
举报
回复
首先你要从文本文件中读出单词,然后找最长的就简单了。
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
int maxline;
char passage[20][80]={0};
int max_lenth=0;
int readdat() /*存入缓冲字符数组*/
{
FILE *fp;
char *p;
int i=0;
if((fp=fopen("in.txt","r"))==NULL)
{
printf("open file error!");
return 0;
}
else {
while(!feof(fp)&&fgets(passage[i],80,fp)!=NULL)
{
p=strchr(passage[i],'\0');
if(p){ *p=0;}
i++;
}
maxline=i;
return 1;
}
}
void trim() /*提取单词*/
{
int i,j=0,k=0;
int n=0;
char temp[20]={0};
for(i=0;i<maxline;i++)
{
while(passage[i][j])
{
temp[n++]=passage[i][j++];
if(!isalpha(temp[n]))
{
temp[n]=0;
n=0;
j++;
if(max_lenth<strlen(temp))
max_lenth=strlen(temp);
}
}
}
}
void main()
{
if(!readdat())
printf("open file error!");
else {
trim();
}
printf("the max_lenth of the txt file is %d",max_lenth);
}
alever513
2004-06-16
打赏
举报
回复
不过这里认为单词与标点之间也有空格分开...
alever513
2004-06-16
打赏
举报
回复
//如果是从文件读入,重定向即可
#include <iostream>
#include <string>
using namespace std;
static int len = 0;
int main()
{
string str;
string strt;
while(cin>>str) {
if(str.length()>len) {
strt = str;
len = str.length();
}
}
// cout<<endl;
// cout<<strt<<endl; //最长单词
return 0;
}
zjxiaoyu
2004-06-16
打赏
举报
回复
这个挺复杂的,特别是关于读文件,取单词那一块.
如果你看过c++ primer的话,那里边有个类似的现成的例子.
如果用c写,挺麻烦.
aheadyes
2004-06-16
打赏
举报
回复
#include"stdio.h"
#include"string.h"
void main(void)
{
char src[100];
char dst[80];
char *p=src;
unsigned int len=0;
printf("Input your string:");
gets(src);
while(1)
{
char temp[80];
unsigned temple;
sscanf(p,"%s",temp);
temple=strlen(temp);
if(len<temple)
{
len=temple;
strcpy(dst,temp);
}
p+=temple;
while(*p==' ')
p++;
if(*p=='\0')
break;
}
printf("the longest string %s:\n", dst);
getchar();
}
一个句子中的最长单词。算法和整个文章的都差不多:
jxnucsb2008
2004-06-16
打赏
举报
回复
高手帮帮忙啊
C语言统计
一篇
英文
短文
中
单词
的个数实例代码
//state 用来记录程序当前是否处于一个
单词
之
中
,初值为0,表示不在
单词
中
,值为1,表示正处于在一个
单词
中
printf("Please input the number of lines for English passage:"); scanf("%d",&n); //输入英语短文...
求
英文
文章
中
单词
的数目
定义赋值一个
英文
字符串代表
一篇
文章
,求
文章
中
单词
的数目(
单词
是以,;或空格分开的)
Flink 快速入门
通过本课程的学习,既能获得Flink企业级真实项目经验,也能深入掌握Flink的核心理论知识,还能获得Flink在生产环境
中
安装、部署、监控的宝贵经验,从而一站式全面、深入掌握Flink技术。 任务作业: 1.使用Eclipse...
用c++写的统计
英文
文章
中
的
单词
个数
用c++写的统计
英文
文章
中
的
单词
个数,输出26个字母的统计频率。
数据结构-3期(KC002) 统计
一篇
英文
短文
中
单词
的个数.docx
数据结构-3期(KC002) 统计
一篇
英文
短文
中
单词
的个数.docx 学习资料 复习资料 教学资源
C语言
69,382
社区成员
243,074
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章