Jump to content

Odd words: Difference between revisions

Added C++ solution
(Added C++ solution)
Line 151:
starvation train
upholstery posey
</pre>
 
=={{header|C++}}==
<lang cpp>#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
 
using word_list = std::vector<std::pair<std::string, std::string>>;
 
void print_words(std::ostream& out, const word_list& words) {
int n = 1;
for (const auto& pair : words) {
out << std::right << std::setw(2) << n++ << ": "
<< std::left << std::setw(14) << pair.first
<< pair.second << '\n';
}
}
 
int main(int argc, char** argv) {
const char* filename(argc < 2 ? "unixdict.txt" : argv[1]);
std::ifstream in(filename);
if (!in) {
std::cerr << "Cannot open file '" << filename << "'.\n";
return EXIT_FAILURE;
}
const int min_length = 5;
std::string line;
std::set<std::string> dictionary;
while (getline(in, line)) {
if (line.size() >= min_length)
dictionary.insert(line);
}
 
word_list odd_words, even_words;
 
for (const std::string& word : dictionary) {
if (word.size() < min_length + min_length/2)
continue;
std::string odd_word, even_word;
for (auto w = word.begin(); w != word.end(); ++w) {
odd_word += *w;
if (++w == word.end())
break;
even_word += *w;
}
 
if (dictionary.find(odd_word) != dictionary.end())
odd_words.emplace_back(word, odd_word);
 
if (dictionary.find(even_word) != dictionary.end())
even_words.emplace_back(word, even_word);
}
 
std::cout << "Odd words:\n";
print_words(std::cout, odd_words);
 
std::cout << "\nEven words:\n";
print_words(std::cout, even_words);
 
return EXIT_SUCCESS;
}</lang>
 
{{out}}
<pre>
Odd words:
1: barbarian brain
2: childbear cider
3: corrigenda cried
4: gargantuan grata
5: headdress hades
6: palladian plain
7: propionate point
8: salvation slain
9: siltation slain
10: slingshot sight
11: statuette saute
12: supersede spree
13: supervene spree
14: terminable trial
 
Even words:
1: cannonball annal
2: importation motto
3: psychopomp scoop
4: starvation train
5: upholstery posey
</pre>
 
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.