题意:现在给你一篇XY学长今天要读的文章,请你写一个程序,输出他都学习到了哪些单词。要求:如果文章中有相同的单词,那么仅仅输出一次;而且如果两个单词只有大小写不同,将他们视为相同的单词。输入一篇文章每组数据以EOF结束。
思路:用set存单词,先把所有非字母的字符转化为空格,然后用stringstream得到每个单词。
#include<bits/stdc++.h>
using namespace std;
int main()
{
set<string> s;
string str;
while (cin >> str)
{
for (int i = 0; i < str.length(); i++)
{
if(isalpha(str[i])) str[i] = tolower(str[i]);
else str[i] = ' ';//把不是字母的符号变为空格
}
string t;
stringstream ss(str);
while(ss >> t) s.insert(t);
}
for(auto it: s)
{
cout << it << endl;
}
return 0;
}
/*
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.
*/