123456789101112131415161718192021222324252627282930313233
std::string str = "enp"; std::string match = ""; int chk = 0; std::vector<std::string> words; words.push_back("the"); words.push_back("ear"); words.push_back("are"); words.push_back("mai"); words.push_back("dad"); words.push_back("mum"); words.push_back("max"); words.push_back("pen"); for (size_t i = 0; i < words.size(); ++i) { for (size_t j = 0; j < str.size(); ++j) { for (size_t k = 0; k < words[i].size(); ++k) { if (str[j] == words[i][k]) { chk++; } } } if (chk != 3) chk = 0; else match = words[i]; } std::cout << match;
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
#include <iostream> #include <string> #include <vector> #include <fstream> #include <algorithm> bool IsEmpty(const std::string str) { for (size_t i = 0; i < str.size(); ++i) { if (str[i] != '\0') return false; } return true; } void LoadFile(const std::string s, std::vector<std::string>& v) { std::string str; std::ifstream file("wordlist.txt"); std::cout << "Loading wordlist\n"; if (file.is_open()) { while (std::getline(file, str)) { if (s.size() == str.size()) { // Converts to lowercase std::transform(str.begin(), str.end(), str.begin(), tolower); // Puts string into vector v.push_back(str); } } file.close(); } else { std::cout << "Error: Cannot load file!\n"; } } int main() { std::string str = "era"; std::string tmp = ""; std::vector<std::string> words; LoadFile(str, words); std::cout << "Original: " << str << '\n'; // Check each character with each other for (size_t i = 0; i < words.size(); ++i) { tmp = words[i]; for (size_t j = 0; j < str.size(); ++j) { for (size_t k = 0; k < tmp.size(); ++k) { if (str[j] == tmp[k]) { tmp[k] = '\0'; break; } } } if (IsEmpty(tmp)) { std::cout << words[i] << '\n'; } } std::cin.get(); return 0; }
sort(a) == sort(b)