C++编程找出字符串中最长和最短的单词

来源:百度知道 编辑:UC知道 时间:2024/05/21 12:16:47
如: string line1= "We were her pride of 10 she named us:";
string line2= "Benjamin, Phoenix, the Prodigal";
string line3= "and perspicacious pacific Suzanne";
string sentence= line1 + ' '+ line2 + ' '+ line3;
要求找出 sentence对象中最长和最短的单词,如果有多个最长或最短的单词,则将它们全部输出
C++初学,请大虾们指点

#pragma warning(disable : 4786)

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void main()
{
const string line1 = "We were her pride of 10 she named us:";
const string line2 = "Benjamin, Phoenix, the Prodigal";
const string line3 = "and perspicacious pacific Suzanne";
const string sentence = line1 + ' ' + line2 + ' ' + line3;

const char *stmp = sentence.data();
vector< string > vect, vecMin, vecMax;
string strtmp = "";
unsigned int i;

//搜索字符串中的所有单词
for(i = 0; i <= strlen(stmp); i++)
{
if(isalpha(stmp[i]))
{
strtmp += stmp[i];
}
else if(!strtmp.empty())
{
vect.push_back(strtmp);
strtmp = "";
}
}

if(!vect.size())
{
cout<<"找不到单词!&q