QT/C++ | Word Unscrambler
QT/C++ | Word_Unscrambler
Hello peeps! Lord Hypersonic greets you. Today I am here with a new Qt project. Yeah, it's a GUI program. Do you remember previous c++ program to solve jumble words? The logic of this program is the same as that program.
- First, the user will be asked to enter the jumbled word in a line edit widget.
- Click the "Solve" button. If the button is clicked, call slot ( on_Button_Solve_clicked() ).
- Clear the list widget.
- Making QString variable QJWord equal to text in line edit widget.
- Declare QString variable sortedJWord and making equal to sortString(QJWord).
- sortString() function work as follows, Function declaration QString sortString (QString word); Defined as :-
- Making integer variable len equal to the size of word (word.length()).
- Starting for loop from i = 0 to i = len, and i=i+1 after each iteration. This loop is for making lower case alpha to uppercase alpha. Using member functions of QString class: isLower and toUpper.
- Declare a string variable w and make it equal to word.toStdString(), to convert QString to string.
- Declaring integer array TEMP[len]. Start a loop to initialize the array with 0.
- Start a for loop from i = 0 to i = len and increase value of i after each iteration. Making TEMP[i] = w[i] to assign TEMP[i] value of ascii code of character at w[i].
- Now sort the array TEMP, you can use any sorting technique to sort array TEMP.
- Declare a string variable J.
- Start a loop from i =0 to i = len (i = i+1 after each iteration). add charater at the end of J at ascii code TEMP[i] ( J.push_back(TEMP[i]) ).
- Convert string to QString using a member function fromStdString(&string). QString Jumble = QString::toStdString(J);
- Return Jumble
- Declare QFile object file. And make the file equal to words.txt (a text file which contains all English words).
- Check if the file is open.
- If the file is open:-
- Declare a text stream [ QTextStream FileLine ] and make it equal to QFile variable file. [ QTextStream FileLine (&file) ].
- Start while loop till end of the text stream.
- Declare a QString variable SFileL and make it equal to FileLine.readline(). To read the file line by line.
- Sort the SFileL using sortString() function. Make QString variable sortedWORD equal to sortString(SFileL).
- If sortedWORD is equal to sortedJWord. Then print it on the screen by adding as an item to list widget. ( ui->listWidget_SOLVED->addItem(SFileL) ).
- Close the file.
- If not able to find words.txt then display a warning message box saying Failed to open words.txt.
Download project files (source code) from GitHub.
Post a Comment