Unscramble program. The language I\'m using is C++. I need help on line 26 and 3
ID: 3576657 • Letter: U
Question
Unscramble program. The language I'm using is C++. I need help on line 26 and 36. I need to input code there in order for the unscramble program to run but I'm unsure how to do this. The rest of the code is just a template.
Write a program to unscramble words A file with list of words is provided along with a template program to get you started. 1. Define a struct with 2 string members: sorted and original 2. Declare an array of 200000 elements with data type of the struct 3. Read from a file a list of words into original member and the sorted version of the word into the sorted member of each element of the array 4. Continue to ask the user (interactively) to type a word or the letter q' to quit. 5. Your program will unscramble and print out all the words with the same letters as the given word. Note: this assignment only requires adding less than 20 lines of code to the template provided.Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string sort(string s);
struct word{
string sorted;
string original;
};
int main(){
const int maxSize = 200000;
static word wordlist[maxSize];
int numWords;
ifstream infile;
infile.open("words.txt");
int i = 0;
string input;
while( infile && i < maxSize )
{
word newWord;
infile >> newWord.original;
newWord.sorted = sort( newWord.original );
wordlist[i++] = newWord;
}
numWords = i;
string w;
do{
bool found = false;
cout << "Please type in a word to unscamble or 'q' to quit: ";
cin >> w;
w = sort(w);
for(int i = 0; i < numWords; i++ ){
if( wordlist[i].sorted == w ){ cout << wordlist[i].original << " "; found = true;}
}
if(!found){ cout << "No matching words were found";}
cout << endl;
}while( w != "q" );
return 0;
}
string sort( string s ){
string t;
while( s != "" ){
int minIndex = 0;
for(int i = 1; i < s.length(); i++){
if( s[i] < s[minIndex] ){
minIndex = i;
}
}
t += s[minIndex];
s.erase( minIndex,1);
}
return t;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.