p218 输出字符串中的最长单词

来源:互联网 发布:win7内存优化软件 编辑:程序博客网 时间:2024/06/03 19:25
#include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<iomanip>#include<cstring>#include<vector>#include<iterator>#define N 10001using 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;}