#include #include #include #include using namespace std;

int main() { string s; getline(cin, s); // 读入整行字符串 stringstream ss(s); // 用 stringstream 分割字符串 vector words; string word; while (getline(ss, word, ' ')) { if (word[word.size()-1] == ',') { // 如果单词末尾是逗号,去除逗号 word = word.substr(0, word.size()-1); } words.push_back(word); // 将单词加入 vector } string longest_word = "", shortest_word = words[0]; // 初始化最长和最短单词 for (string w : words) { if (w.size() > longest_word.size()) { // 更新最长单词 longest_word = w; } if (w.size() < shortest_word.size()) { // 更新最短单词 shortest_word = w; } } cout << longest_word << endl << shortest_word << endl; return 0; }

C++ 编程练习:查找最长和最短单词

原文地址: https://www.cveoy.top/t/topic/n6s7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录