题目描述:
在 D:\\doucment.text 文本中,文件中有若干行类文文本,每行英文文本中有若干 个单词,每个单词不会跨行出现,每行至多包含 100 个字符,要求编写一个程序,处理文 件,分析各行中的单词,找到每行中的最长单词,分别输出其长度。如:dictionary--->10。
#include<bits/stdc++.h>
using namespace std;
int main(){
fstream a;
int i=0,max,count;//max为最长单词长度。
string c[100],s,maxs;//maxs为最长单词。
a.open("C:/Users/29633/Desktop/test.txt",ios::in);//打开文件
while(getline(a,c[i]))i++;//逐行地读并存储到数组c中。
for(int k=0;k<i;k++)//对c[i]进行遍历
{max=0,count=0,s="";//重置计数
c[k]=c[k]+' ';//在末尾加空格,就无需判断字符串c[k]最后一个单词了,因为就只需判断遍历的数是否为' ';
for(int t=0;t<c[k].size();t++)//每一行遍历
{
if(c[k][t]==' '){
if(count>max)
max=count,maxs=s;
count=0,s="";}//重置计数
else
count++,s=s+c[k][t];
}
cout<<max<<"--->"<<maxs<<endl;
}
a.close();
return 0;
}