Algorithm searching in strings with wildcards (*,?) in C
Go to solution
Solved by rednas16,
this is the bug free code
//0 = no match//1 = match//When pattern is "" will return a matchint matchesPattern( char* input, char* pattern) { int i, z; if(pattern[0] == '\0') return 1; for(i = 0; pattern[i] != '\0'; i++) { if(pattern[i] == '\0') { return 0; //Pattern has ended but still some left, meaning no match } else if(pattern[i] == '?') { continue; //? replaces a character, no need to check if there is a match } else if(pattern[i] == '*') { //Pattern has matched up until now, but now need to check //the rest of the pattern //Checking for the match of pattern starting after the * //If the match isn't found, shift input by 1 and check again //If no match is found then pattern doesn't exist for(z = i; input[z] != '\0'; z++) { if(matchesPattern(input + z, pattern + i + 1) == 1) { //Pattern found, return true return 1; } } //If this line was hit the pattern after * couldn't be found anywhere return 0; } else if(pattern[i] != input[i]) { return 0; //Didn't match input } //Characters matched, keep continuing } //To hit here the current pattern must contain no * and must have all characters matching return 1;}
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now