In C++, I\'m trying to let the user input 8 letters, and have the program place
ID: 638786 • Letter: I
Question
In C++, I'm trying to let the user input 8 letters, and have the program place them all into an array. After that's done, The program is supposed to open a new function that reads a list of words from a text file (maybe putting these words into their own array), comparing these words with the letters that have been entered, and printing out the words from the list that can be created using the 8 previously inputted letters.
I know that you can use a loop to put all of the letters into an array (i=0,i<8,i++), but what about the other aspects? Help!
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int hashmap[26];
void check(string s)
{
int i=0,l;
l=s.length();
while(i<l)
{
char c=s[i];
c=tolower(c); //if any uppercase is there, convert it to lower case
int j=c-97; //assci value of'a' starts from 97
if(hashmap[j]==1)
i++;
else
break;
}
if(i==l)
cout<<" "<<s;
}
void mapping(char *a)
{
for(int i=0;i<8;i++)
{
char c=a[i];
c=tolower(c); //if any uppercase is there, convert it to lower case
int j=c-97; //assci value of'a' starts from 97
hashmap[j]=1; // mark the respect postion to 1 indicating that it is one of the 8 letters
}
}
int main()
{
char a[8];
cout<<"enter 8 letters: ";
for(int i=0;i<8;i++)
cin>>a[i];
mapping(a); // calling mapping function
ifstream file;
file.open("words.txt"); //opening the file
string s;
cout<<" words which can be formed from 8 letters are: ";
while(!file.eof()) //while not the end of file
{
file>>s;
check(s); //calling check function, if the word read from file, can be made from the 8 letters or not
}
return 0;
}