输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出,单词不区分大小写。
输入样例
Adventures in Disneyland Two blondes were going to Disneyland when they came to a fork in the road. The sign read: "Disneyland Left." So they went home.
输出样例
a adventures blondes came disneyland fork going home in left read road sign so the they to two went were when
代码
#include<iostream> #include<algorithm> #include<sstream> #include<string> #include<set>//因为可能有重复的单词,而set可以去重,使用起来更好。 using namespace std; string str,str2; stringstream ss; set<string> jh; int main() { while (cin >> str ) { for (int i = 0; i < str.length(); i++) {//去除非单词字符 if (isalpha(str[i])) { str[i] = tolower(str[i]); } else { str[i] = ' '; } } ss << str; //每次得清空ss //stringstream ss(str); 如果这样写就不用每次清空ss了. while (ss >> str) jh.insert(str); ss.clear();//这里必须清空...具体原因为止. } for (set<string>::iterator it = jh.begin(); it != jh.end(); it++) { cout << *it << endl; } return 0; }