Hello
I have a function that takes in a 'word pattern' & a word & determines if the word contains the same characters in the same position as the 'word pattern'.
For eg;
'word pattern' = he???
word = hello- The function will return true because the word variable matches the pattern of the 'word pattern'
eg 2
'word pattern' = ma??h
word = hello- The function will return false because the word doesn't match the 'word pattern'
My Problem is when I use the function, it doesn't work, it returns false for everything even when it is given a correct word pattern & word which should return true.
bool wordPattern(string w1, string w2) {
for (int i=0; i<(int)w1.size();i++)
{
if ( isalpha(w1[i]) ) { // if this character is not a '?'
size_t val= w2.find(w1[i],i);
if(val==string::npos) { return false; } // if we find that a character from
// w1 is not present in w2 at position i
// then return false;
}
}
return true;
}
The way I use this string is I search an 'array of words' for words that have the same length as the 'word pattern' then I implement the function wordPattern to check if they have the same pattern.
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";
}
}
}