Created
February 26, 2021 17:57
-
-
Save inxanedev/296cca9ebd4c30eaf48fc126e95e7658 to your computer and use it in GitHub Desktop.
C++ program to find the longest word that's displayable on a seven segment display.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <vector> | |
int main(int argc, char** argv) { | |
if (argc == 1) { | |
std::cout << "Usage: " << argv[0] << " <word list path>" << std::endl; | |
return -1; | |
} | |
std::string allowed_chars = "abcdefhijlnoprstuy"; | |
std::vector<std::string> valid_words; | |
std::ifstream wordlist(argv[1]); | |
std::string current_word; | |
while (std::getline(wordlist, current_word)) { | |
bool valid_char = true; | |
for (int i = 0; i < current_word.length() - 1; i++) { | |
if (allowed_chars.find(current_word[i]) == std::string::npos) { | |
valid_char = false; | |
break; | |
} | |
} | |
if (valid_char) { | |
valid_words.push_back(current_word); | |
} | |
} | |
int longest_index = 0; | |
for (int i = 0; i < valid_words.size(); i++) { | |
if (valid_words[i].length() - 1 > valid_words[longest_index].length() - 1) { | |
longest_index = i; | |
} | |
} | |
std::cout << valid_words[longest_index] << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment