Hello all. I am just starting to learn C++ but have moderate experience with C. I am using the Dev-C compiler for my program.
The program I wish to create will allow the user to input a string of words each separated by a space.
It then separates these into different words e.g. "hi every person" would be split into three variables
w1 = "hi";
w2 = "every";
w3 = "person";
Ok I am not worried about this part at the moment as it currently will just take in the one word. It then has to search for this within a predefined wordlist and return whether or not it is found.
I have got this working to. The problem I have run into is as follows:
My program needs to be able to take each word and swap the letters around in every possible combination and then search for this in the wordlist (basically a descrambler).
For example the user enters: "eli"
The program then needs to search for:
"eli"
"eil"
"iel"
"ile"
"lei"
"lie" <- this is the correct word and once found it will return this as the word.
How do I program it to search for each letter in turn regardless of the amount of letters? (Yes i know high amount of letters will take a long time but at the moment it will be limited to 10 or under).
The following is the code used so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream> //file handling
using namespace std;
int check(string word)
{
string line = "";
cout << "\n";
ifstream file("C:/Users/Chazz & Bill/Documents/prog/input/wordlist.txt", ios::in); //open file
if (!file.is_open()) //if cant open file: close
{
cout << "\nUnable to open file \n";
system("PAUSE");
exit(1);
}
while(getline(file,line))
{
if (line == word)
{
cout << "Result found :)\n\n";
return 1;
}
}
if (line != word)
{
cout << "Result NOT found :'(\n\n";
return 0;
}
}
int result(string word)
{
int len = word.length(); //length of string
char num[len]; //how many chars there are
int i = 0;
cout << "\n";
for (i=0;i <=len; i++) //set each char into an array
{
num[i] = word[i];
}
for (i=0;i <=len; i++) //print out each number
{
cout << num[i] << "\n";
}
//****start print in all directions THIS IS WHERE IT NEEDS TO DESCRAMBLE ****/
check(word); //check word exists
//end
return len; //return number of letters
}
int main(int argc, char *argv[])
{
string input = "";
string out = "";
int len = 0;
cout << "Please enter string and press enter:";
cin >> input;
len = result(input);
cout << input << " is " << len << " chars \n";
system("PAUSE");
return EXIT_SUCCESS;
}
|
Thank you for any help you can provide :)