题目描述:
输入1行句子(不多于200个单词,每个单长度不超过100),只包含字母、空格和逗号。单词由至少一个连续的字母构成,空格和逗号都是单词间的间隔。
试输出第1个最长的单词和第1个最短单词。
输入描述:
一行句子。
输出描述:
两行输出:第1行,第一个最长的单词;第2行,第一个最短的单词。
样例输入:
I am studying Programming language C in Peking University
样例输出:
Programming
I
代码如下:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<istream>
#include<iomanip>
#include<ostream>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<fstream>
#include<stack>
#include<ctime>
#include<deque>
#include<queue>
#include <sstream>
#pragma warning (disable:4996)
using namespace std;
int main()
{
string sentence;
getline(cin, sentence);
string longest_word, shortest_word;
int longest_length = 0, shortest_length = 101;
string word;
for (int i = 0; i < sentence.length(); i++) {
if (sentence[i] == ',' || sentence[i] == ' ') {
if (word.length() > 0) {
if (word.length() > longest_length) {
longest_length = word.length();
longest_word = word;
}
if (word.length() < shortest_length) {
shortest_length = word.length();
shortest_word = word;
}
word.clear();
}
}
else {
word += sentence[i];
}
}
if (word.length() > 0) {
if (word.length() > longest_length) {
longest_length = word.length();
longest_word = word;
}
if (word.length() < shortest_length) {
shortest_length = word.length();
shortest_word = word;
}
}
cout << longest_word << endl;
cout << shortest_word << endl;
return 0;
}