从文件流读取文件,输出文本中每行最长的单词及其长度

`

include

include

include

using namespace std;
int main()
{
int n;

string *lineWords=new string[n];
int index=0;
string line;
fstream Englishfile;
Englishfile.open("D:\\study\\Cpp892\\programfiles\\EnglishWords.txt",ios::in);
while(getline(Englishfile,line))
{
    lineWords[index++]=line;
}
for(int i=0;i<index;i++)
{
    int maxlen=0;
    int templen=0;
    string maxwords;
    int start=0;
    int endword=0;
    int flag=-1;
    for(int j=0;j<lineWords[i].length();j++)
    {
        if((lineWords[i][j]>='a' && lineWords[i][j]<='z') || (lineWords[i][j]>='A' && lineWords[i][j]<='Z'))
        {
            if(flag==-1) start=j;
            templen++;
            flag=1;
        }
        else
        {
            if(flag==1)
            {
                if(maxlen<templen)
                {
                     endword=j;
                     maxlen=templen;
                     maxwords=lineWords[i].substr(start,endword-start);
                }

            }
            flag=-1;templen=0;start=0;endword=0;
        }
    }
    cout<<maxwords<<"---"<<maxlen<<endl;
}

}
`

posted @ 2021-12-18 01:03  BruceWhisky  阅读(107)  评论(0)    收藏  举报