aboutsummaryrefslogtreecommitdiff
path: root/challenge-005/paulo-custodio/cpp/ch-1.cpp
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-06-19 21:14:03 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-06-19 21:14:03 +0100
commitd5d9907992a2d62978c0b511d0afb18b4013cf8d (patch)
tree21f1feefc59dd3281151b60e8e4b5acacafbd2f2 /challenge-005/paulo-custodio/cpp/ch-1.cpp
parenteda544efa917e0b554c55cdcc155c19f836ebb4b (diff)
downloadperlweeklychallenge-club-d5d9907992a2d62978c0b511d0afb18b4013cf8d.tar.gz
perlweeklychallenge-club-d5d9907992a2d62978c0b511d0afb18b4013cf8d.tar.bz2
perlweeklychallenge-club-d5d9907992a2d62978c0b511d0afb18b4013cf8d.zip
Add C and C++ solution to challenge 005
Diffstat (limited to 'challenge-005/paulo-custodio/cpp/ch-1.cpp')
-rw-r--r--challenge-005/paulo-custodio/cpp/ch-1.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-005/paulo-custodio/cpp/ch-1.cpp b/challenge-005/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..38ba087c22
--- /dev/null
+++ b/challenge-005/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,46 @@
+/*
+Challenge 005
+
+Challenge #1
+Write a program which prints out all anagrams for a given word. For more
+information about Anagram, please check this wikipedia page.
+create a hash of all words in dictionary where key is sorted list of letters
+therefore two anagrams have the same key
+*/
+
+#include <algorithm>
+#include <cctype>
+#include <fstream>
+#include <iostream>
+#include <string>
+using namespace std;
+
+void strtolower(string& str) {
+ for (size_t i = 0; i < str.size(); i++)
+ str[i] = tolower(str[i]);
+}
+
+void word_key(string& key, const string& word) {
+ key = word;
+ sort(key.begin(), key.end());
+}
+
+int main(int argc, char* argv[]) {
+ string key1, key2, line;
+
+ if (argc != 2) return EXIT_FAILURE;
+ word_key(key1, argv[1]);
+ strtolower(key1);
+
+ ifstream ifs("words.txt");
+ if (!ifs.is_open()) return EXIT_FAILURE;
+
+ while (getline(ifs, line)) {
+ strtolower(line);
+ word_key(key2, line);
+ if (key1 == key2)
+ cout << line << endl;
+ }
+
+ return EXIT_SUCCESS;
+}