Write a game Program in C++ called \"Word Ladder\", that answers the game requir
ID: 3767166 • Letter: W
Question
Write a game Program in C++ called "Word Ladder", that answers the game requirment below.
1.use four letter words only
2.Given two words of identical length, what is the shortest "Word Ladder" that you can construct between them? for example, you can go from HATE to LOVE in three steps: HATE - LATE- LAVE - LOVE, or HATE - HAVE - HOVE - LOVE, etc...
3. Implement a graph for the word ladders on which you will perform breadth-first search. Either use the STL or write your own classes. Feel free to use list, array, stack or queue, but write your own graph class.
4. Print the successful output for this game. Make sure it is working in C++ IDE.
Data source for 4 letter words in English
http://www.scrabble.org.au/words/fours.htm
Explanation / Answer
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string start, last;
while (true) {
cout << "Enter first four-letter word" << endl;
cin >> start;
if (start.length() == 4)
break;
else
cout << "Not a four-letter word. Enter again" << endl;
}
while (true) {
cout << "Enter second four-letter word" << endl;
cin >> last;
if (last.length() == 4)
break;
else
cout << "Not a four-letter word. Enter again" << endl;
}
transform(start.begin(), start.end(), start.begin(), ::toupper);
transform(last.begin(), last.end(), last.begin(), ::toupper);
int counter = 0;
cout << start << "-";
for (int i = 0; i < 4; i++) {
if (start.at(i) == last.at(i))
continue;
else {
start[i] = last[i];
cout << start;
if (start.compare(last) != 0)
cout << "-";
counter++;
}
}
cout << endl << "Number of steps = " << counter << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.