C++字符串中最长的字母子串
来源:百度知道 编辑:UC知道 时间:2024/06/21 19:16:54
编写程序求出所给出的字符串最长的字母子串(以非字母隔开)例如:字符串"Appp$ddna weartner & # bana"中最长的字母子串为wearther
#include<cctype>
#include<string>
#include<iostream>
using namespace std;
void main()
{
string line;
char ch;
int start = 0;
int left = 0;
int tem = 0;
int length = 0;
getline(cin,line);
line += "#";
for(int i = 0; i < line.size(); i++)
{
ch = line.at(i);
if( isalpha(ch) )
{
tem++;
}
else
{
if(tem > length)
{
start = left;
length = tem;
}
left = i+1;
tem = 0;
}
}
line.erase(line.end()-1);
cout<<"最长单词(应该是字母串)"<<line.substr(start,length)<<endl;
}
头文件<cctype>中有函数isalph()判断是不是字母。