Thanks for your help guys
I've got the major parts sorted out, identify if a word fits the word pattern, I just dont know how to search the Array of words
efficiently(timewise).
I have an array that is 130,000 words, yes thats right, 130,000, & the words are all in alphabetical order. So would you guys suggest a binary search is the way to go, or is there a different way to search the array I dont know of.
Could anyone help me alter my
search Function from a
selection sort to a
Binary search?
Below is my code which uses a selection sort/search method:
void search(string array[], int a_size, string w) {
// array[] = an array of words in alphabetical order
// a_size = array's size which is over 130,000
for (int i=0; i<a_size; i++) {
if (array[i].length() == w.length()) {
if (wordPattern(w,array[i]))
cout << array[i] << "\n";
}
}
}
bool wordPattern(string w1, string w2) {
for (int i=0; i<(int)w1.size();i++)
{
if (w1[i]!='?') {
size_t val= w2.find(w1[i]);
if(val==string::npos) { return false; }
else { w2.erase(val,1); }
}
}
return true;
}
Entire Program
#include <iostream>
#include <string>
using namespace std;
void to_lower(string& a);
void search(string array[], int a_size, string w);
bool wordPattern(string w1, string w2);
int main() {
string word;
string a[] = {"feig", "frab", "frae", "fram", "frap", "frat", "fray", "freedom", "fret", "fribalis"};
cout << "Enter a word pattern: " << flush;
cin >> word;
to_lower(word);
search(a,10,word);
return 0;
}
void to_lower(string& s) {
for (int i=0; i<(int)s.length(); i++) {
s[i] = tolower(s[i]);
}
}
void search(string array[], int a_size, string w) {
for (int i=0; i<a_size; i++) {
if (array[i].length() == w.length()) {
if (wordPattern(w,array[i]))
cout << array[i] << "\n";
}
}
}
bool wordPattern(string w1, string w2) {
for (int i=0; i<(int)w1.size();i++)
{
if (w1[i]!='?') {
size_t val= w2.find(w1[i]);
if(val==string::npos) { return false; }
else { w2.erase(val,1); }
}
}
return true;
}