输入一段英语文本,找出所有不同的单词,按字典序从小到大输出,单词不区分大小写。
【分析】
需要对输入进行预处理,因为包含标点符号等字符,需要将不是字母的字符变为空格,再将每个单词全部变为小写,用 set 统计即可
stringstream 参考链接:https://blog.csdn.net/u013186902/article/details/50853217
#include <iostream>
#include <string>
#include <set>
#include <sstream>
using namespace std;
set<string> dict;
int main() {
string s, buf;
while (cin >> s) {
for (int i = 0; i < s.length(); i++) {
if (isalpha(s[i])) {
s[i] = tolower(s[i]);
} else {
s[i] = ' ';
}
}
stringstream ss(s);
while (ss >> buf) {
dict.insert(buf);
}
}
set<string> :: iterator i;
for (i = dict.begin(); i != dict.end(); i++) {
cout << *i << endl;
}
return 0;
}
京公网安备 11010502036488号