Comparing string with wildcard characters

Hi,

Working on some code that compares strings, currently trying to implement a function that will recognize '_' as a wildcard so that for instance, c_t would match cat or cut.

I currently have it string comparing code working but are not sure on how to progress it to include wildcards, do i need to store the string characters in an array and then iterate through them testing one at a time?

Any suggestions, pseudo code appreciated.

1
2
3
4
  if (pattern2 == word2) {
                    matches += 1;
                    cout << word << endl;
                }
So this is what I am currently trying:

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
int main(int argc, char** argv) {


    string pattern;
    pattern = "c_t";
    string word;
    word = "catt";
    bool match = true;


    for (int i = 0; i = pattern.size(); i++) {
        if (pattern.at(i) != word.at(i) || pattern.at(i) != '_') {
            match == false;
            cout << "yep" << endl;
        }

    }
    if (match == true)
        cout << word << endl;

    return 0;
}



Got it! was using and or (||) statement instead of &&
Cool!

I assume you mean this on line 11

i < pattern.size()

(rather than =)

And line 13 should be:

match = false; // set match to false, rather than compare it?

Andy

PS It is better form in C++ to initialize variables on a single line (as you do for match.)

string pattern = "c_t"; // construct with value

cf.

1
2
string pattern; // default constructor
pattern = "c_t"; // assign value using operator= 
Last edited on
Topic archived. No new replies allowed.