求出一篇英文文章中单词最长的单词

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
  • 打赏
  • 举报
回复
高手帮帮忙啊

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧