#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<vector>
#include<iterator>
#define N 10001
using namespace std;
int kong(char e) //判断是否是字母
{
if(e>='a' && e<='z' || e>='A' && e<='Z')
return 1;
else
return 0;
}
int chang(char e[])
{
int c=strlen(e), i, len=0, longest=0, point, flag=1, dian;
for(i=0; i<=c; i++){
if(kong(e[i])){
if(flag){
point = i;
flag = 0; //记录单词的长度
}
else
len++;
}
else{
flag =1;
if(len>=longest){
longest = len;
dian = point; //记录下最长单词的首位置
len = 0;
}
}
}
return dian;
}
int main()
{
char e[100000];
cout << "请输入一串字符:";
gets(e);
cout << "最长的单词是:";
for(int i=chang(e); kong(e[i]); i++){
cout << e[i];
}
cout << "\n";
return 0;
}
p218 输出字符串中的最长单词
最新推荐文章于 2021-11-07 09:00:00 发布