#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
const string PUNCTUATION = "!.,;:\"'";
// normalize converts everything to lower case and
// and removes trailing puncutation
string normalize(string word) {
string newword;
for (char c : word) { // loop through the string
if (isupper(c)) { // find upper case stuff
newword += tolower(c); // convert to lower case
}
else {
newword += c;
}
}
// find the index of the last letter that is not punctuation
unsigned long end_of_word = newword.find_last_not_of(PUNCTUATION);
// if there is no such letter (the word is all punctuation)...
if (end_of_word == string::npos) {
return ""; // return an empty string
}
// return (end_of_word+1) letters starting at the beginning
return newword.substr(0,end_of_word+1);
}
// look to see if the word is already in the list
bool already_there(string word, vector<string> list) {
for (auto w : list) {
if (w == word) {
return true;
}
}
return false;
}
int main()
{
vector<string> words;
string word;
string line;
cout << "Give me some words: (type no if you do not want to) ";
getline(cin, line); // grab the whole line
if ("no" != line) { // if the whole line is NOT no...
stringstream temp_stream(line); // put the line in a temp stringstream
while (temp_stream >> word) { // grab words one at a time
words.push_back(word); // put them in the vector
}
// print out the words:
cout << "The words you gave me: ";
for (auto w : words) {
cout << w << " ";
}
cout << endl;
// Find the UNIQUE words:
vector<string> uwords;
for (auto w : words) { // look through all of the words
string temp_word = normalize(w); // normalize each one
if (!already_there(temp_word, uwords)) { // if it is NOT already in the list of unique words
uwords.push_back(temp_word); // then add it to the unique words
}
}
// print out the unique words:
cout << "Unique words: ";
for (auto w : uwords) {
cout << w << " ";
}
cout << endl;
}
system("pause");
return 0;
}
Back to top