#include<string>
#include<iostream>
#include<vector>
int main()
{
using namespace std;
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;
string space(" ");
string::size_type pos , start, send, wordlength,ncount=0,maxlen,minlen;
pos = start = send = wordlength = 0;
vector<string> max,min;
string word;
while ((start = sentence.find_first_not_of(",:", send)) != string::npos)
{
ncount += 1;
send = sentence.find_first_of(" ", send);
if (send == string::npos)//当前处于最后一个单词
{
wordlength = sentence.size() - start;
word.assign(sentence.begin() + start, sentence.begin() + start+ wordlength);
}
else
{
if (isdigit(sentence[start]))
{
send += 1;
ncount -= 1;
continue;
}
wordlength = send - start;//单词的长度
if (ispunct(sentence[send - 1]))
wordlength -= 1;
cout << word.assign(sentence.begin() + start, sentence.begin() + start+ wordlength) << endl;
send += 1;
}
if (ncount == 1)
{
maxlen = minlen = wordlength;
max.push_back(word);
min.push_back(word);
}
if (wordlength > maxlen)
{
maxlen = wordlength;
max.clear();
max.push_back(word);
}
else if (wordlength == maxlen)
max.push_back(word);
if (wordlength < minlen)
{
minlen = wordlength;
min.clear();
min.push_back(word);
}
else if (wordlength == minlen)
min.push_back(word);
}
printf("有%d个单词!\n", ncount);
vector<string>::iterator itermax = max.begin();
cout << "单词最长的有:\n";
while (itermax!=max.end())
{
cout << *itermax++ << " ";
}
cout << endl;
cout << "单词最短的有:\n";
vector<string>::iterator itermin = min.begin();
while (itermin != min.end())
{
cout << *itermin++ << " ";
}
cout << endl;
system("pause");
return 0;
}
#include<iostream>
#include<vector>
int main()
{
using namespace std;
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;
string space(" ");
string::size_type pos , start, send, wordlength,ncount=0,maxlen,minlen;
pos = start = send = wordlength = 0;
vector<string> max,min;
string word;
while ((start = sentence.find_first_not_of(",:", send)) != string::npos)
{
ncount += 1;
send = sentence.find_first_of(" ", send);
if (send == string::npos)//当前处于最后一个单词
{
wordlength = sentence.size() - start;
word.assign(sentence.begin() + start, sentence.begin() + start+ wordlength);
}
else
{
if (isdigit(sentence[start]))
{
send += 1;
ncount -= 1;
continue;
}
wordlength = send - start;//单词的长度
if (ispunct(sentence[send - 1]))
wordlength -= 1;
cout << word.assign(sentence.begin() + start, sentence.begin() + start+ wordlength) << endl;
send += 1;
}
if (ncount == 1)
{
maxlen = minlen = wordlength;
max.push_back(word);
min.push_back(word);
}
if (wordlength > maxlen)
{
maxlen = wordlength;
max.clear();
max.push_back(word);
}
else if (wordlength == maxlen)
max.push_back(word);
if (wordlength < minlen)
{
minlen = wordlength;
min.clear();
min.push_back(word);
}
else if (wordlength == minlen)
min.push_back(word);
}
printf("有%d个单词!\n", ncount);
vector<string>::iterator itermax = max.begin();
cout << "单词最长的有:\n";
while (itermax!=max.end())
{
cout << *itermax++ << " ";
}
cout << endl;
cout << "单词最短的有:\n";
vector<string>::iterator itermin = min.begin();
while (itermin != min.end())
{
cout << *itermin++ << " ";
}
cout << endl;
system("pause");
return 0;
}