C++ | Unjumble the Jumbled Words
Hello peeps! Lord Hypersonic greets you. Today I am here with a c++ program that will solve any jumble word. ONLY WORD.
For working of this program, you need a text file in the same folder (click here to download files), in which EXE file is.
So the working of the program is like this;
1. First, the user will be asked to enter a jumbled word.
2. After that, it will convert the lower case alpha to upper case alpha, then sort the string, means if you
give "ollhe" then after sorting it will look like this: "EHLLO"
3. After sorting it will open the text file (words.txt) which contain all English words (download link is given above).
4. Then it will copy the words from file to string line (reading line by line).
5. Then it will sort the line (convert the lower case alpha to upper case alpha, then sort the word, SAME AS DONE ABOVE), and check both the strings i.e, sorted jumbled words and sorted word from the file. If both strings are equal then that word from the file is the answer.
6. Following these steps, it will print all possible answers.
Source Code:-
/*
Program: Solve Jumble Words
Description: This program will solve the jumble words and will will print all the possible answers for the words. like if you give lacpsie then it will print plaices and special
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blogspot.in
*/
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
//function to sort the string
string sortString(string word)
{
int len=word.length(); //length of given word
for(int i=0; i<len; i++) // loop for converting lower alpha to upper alpha
{
if(islower(word[i])) word[i]=toupper(word[i]);
}
string Jumble;
int TEMP[len];
for(int i=0; i<len; i++) TEMP[i]=0; //initializing array TEMP with 0.
for(int i=0; i<len; i++) //loop for storing ascii code of alphabets in given word
{
TEMP[i]=word[i];
}
for(int i=0; i<len; i++) // loop for sorting numbers
{
for(int j=0; j<len; j++)
{
if(TEMP[j]>TEMP[j+1])
{
int temp=TEMP[j];
TEMP[j]=TEMP[j+1];
TEMP[j+1]=temp;
}
}
}
for(int i=0; i<len; i++) //loop for creating sorted string of given word like: dance will be ACDEN
{
Jumble.push_back(TEMP[i]);
}
return Jumble; //returning sorted string
}
int main()
{
while(1) {
string line="",jumble="",Jb="";
int c=1; // counter
cout<<"Enter jumbled word: "; getline(cin,jumble); //asking for jumble words
Jb=sortString(jumble); //sorting jumble word
ifstream words("words.txt"); //opening words.txt wordlist
cout<<"\nPossible words are:- "<<endl;
if(words) //if file is open
{
while(getline(words,line)) // loop until end of file, copying string in words to line
{
string Wds=sortString(line); //sorting word in file
if(Wds==Jb) //if sorted word is equal to sorted jumble word then it is the answer
{
cout<<c++<<". "<<line<<endl; //printing answer.
}
}
words.close();//closing words.txt
_getch();
cout<<endl<<endl;
}
else //if not able to find word.txt
{
cout<<"\nwords.txt is missing........... can not do further processing :( \n";
_getch();
}
}
return 0;
}
NOTE: words.txt SHOULD BE IN THE SAME FOLDER IN WHICH THE EXE OF PROGRAM IS, OTHERWISE IT WILL NOT WORK.
Post a Comment