Given the word find puzzle below locate and print out all four letter words. The
ID: 3713768 • Letter: G
Question
Given the word find puzzle below locate and print out all four letter words. The words can appear in their normal form or reversed and both horizontal or vertical. (no diagonal words) Make an array of strings with dimension 10. Read all ten lines of the puzzle into the array. Then process one line at a time. When finished with the rows take each letter from a column and make a new ten letter line. You can then use the same code to find the words in the column.You should have a function that determines if a sequence of letters constitutes a word. You may want to use the binary search algorithm to search the dictionary of four letter words. The following puzzle contains ten lines with ten letters in each line.
Puzzle:
prubackegs
esoreeated
emoodlcent
ktllablead
msrkradmaf
flootijotv
sarxstmice
nmtalktsud
oxquitxmub
datasetlas
Explanation / Answer
1) word_puzzle.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
string reverseit(string line);
int main(void)
{
string test_word = "";
string reversed_test_word = "";
string puzzle_line = "";
string puzzle_low = "";
string dictionary_word = "";
unsigned int j, k;
char ch;
bool found = false;
ifstream dictionary;
ifstream puzzle;
puzzle.open("D:/word_puzzle.txt");
if (!puzzle)
{
cout << "Unable to open puzzle file" << endl;
return 1;
}
else
{
cout << "Puzzle file opened" << endl;
}
dictionary.open("D:/dictionary_four_letter_words.txt");
if (!dictionary)
{
cout << "Unable to open dictionary file" << endl;
return 1;
}
else
{
cout << "Dictionary file opened" << endl << endl;
}
// Get a line from the puzzle
while (!puzzle.eof())
{
puzzle >> puzzle_line;
cout << endl << puzzle_line << endl;
// Make sure it is lowercase.
puzzle_low = "";
for (j = 0; j < puzzle_line.length(); j++)
{
ch = tolower(puzzle_line.at(j));
puzzle_low = puzzle_low + ch;
}
// Extract 4 characters
for (k = 0; k < puzzle_low.length() - 3; k++)
{
test_word = puzzle_low.substr(k,4);
// Make a reverse copy of the test_word
reversed_test_word = reverseit(test_word);
// Now go through the whole dictionary to see if these are words
dictionary >> dictionary_word; // Get one word from the dictionary
while(!dictionary.eof())
{
if (dictionary_word == test_word)
{
cout << test_word << " is a word" << endl;
}
if (dictionary_word == reversed_test_word)
{
cout << reversed_test_word << " is a word" << endl;
}
dictionary >> dictionary_word; // Get next word from the dictionary
} // End dictionary search loop
// Reset the dictionary file to test the next words.
dictionary.clear();
dictionary.seekg(0, ios::beg);
} // End loop for processing one line of the puzzle
} // End loop for testing each line of the puzzle
cout << endl << endl << endl;
return 0;
}
//
// Function Reversit
//
string reverseit(string line)
{
string invert_line = "";
int position = 0;
for (position = line.length() - 1; position >= 0; position--)
invert_line += line.at(position);
return invert_line;
}
2) word_puzzle.txt
prubackags
esoreeated
emoodlcent
ktllablead
msrkradmaf
flootijotv
sarxstmice
nmtalktsud
oxquitxmub
datasetlas
3) dictionary_four_letter_words.txt
back
kegs
rose
sore
eros
mood
doom
cent
ball
able
lead
dark
loot
tool
mice
talk
dust
quit
data
salt
peek
keep
dons
slam
dart
trad
arts
able
bait
teem
meet
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.