C++ program to check if two strings match where one string contains wildcard characters
Checking if two strings match where one String contains wild characters
In this article, we will learn how to write a C++ program to check if two strings match where one string contains wildcard characters. Wildcard characters are those that can be compared with one or more characters. ‘*’ and ‘?’ are typical wildcard characters where ‘*’ can be compared with zero or more characters (string, str*ng,s*g are same )and ‘?’ can be compared with only one character (string, str?ng are same). In this program, we will make a function check() with boolean as a return type, that will return true if the strings match else it will return false.
Algorithm:
- Initialize the variables.
- Accept the input.
- Check both the strings character by character.
- If a character of wild string contains a ‘*’, take the next character from wild string and check for that character in string which you want to match.
- If they match, continue checking the next character
- Else return false.
- If the character of wild string contains a ‘?’, check if the immediate next character of wild string and the next character of string to be matched.
- If they match, continue checking the next characters.
- Else, return false.
- Print result.
C++ program (String contains wild characters)
Run
#include<iostream> using namespace std; int main() { //Initialize the variables. string wild = "Prep*nsta", str = "Prepinsta"; //Accept the inputs. cout << "String containing wild characters: "; cout << wild << endl; cout << "String to be matched: "; cout << str << endl; bool TRUE = true, FALSE = false; bool check[wild.length() + 1][str.length() + 1]; check[0][0] = TRUE; for (int i = 1; i <= str.length(); i++) check[0][i] = FALSE; for (int i = 1; i <= wild.length(); i++) if (wild[i - 1] == '*') //Checking for wild characters. check[i][0] = check[i - 1][0]; else check[i][0] = FALSE; for (int i = 1; i <= wild.length(); i++) { for (int j = 1; j <= wild.length(); j++) { if (wild[i - 1] == str[j - 1]) check[i][j] = check[i - 1][j - 1]; else if (wild[i - 1] == '?') //Checking for wild character '?'. check[i][j] = check[i - 1][j - 1]; else if (wild[i - 1] == '*') //Checking for wild character '*'. check[i][j] = check[i - 1][j] || check[i][j - 1]; else check[i][j] = FALSE; } } //Printing result. if (check[wild.length()][str.length()]) cout << "TRUE"; else cout << "FALSE"; }
Output
String containing wild characters: Prep*nsta String to be matched: Prepinsta TRUE
Login/Signup to comment
#include
#include
#include
using namespace std;
bool checkwildchar(char str1[],char str2[]){
int s{0},dec{0};
while(str2[s] != ‘\0’){
if(str2[s] == ‘*’){
dec = 1;
break;
}
++s;
}
if(dec == 0){
int x{0},count{0},flag{0};
while(str1[x] != ‘\0’){
if(str1[x] == str2[x])
count++;
else if(str2[x] == ‘?’){
flag++;
count++;
if(flag >= 2)
return false;
}
else if(str1[x] != str2[x] && str2[x] != ‘?’)
return false;
x++;
}
if(count == strlen(str1))
return true;
}
else{
int z{0},y{0},count{0};
while(str1[z] != ‘\0’){
if(str1[z] == str2[y]){
count++;
z++;
y++;
}
else if(str2[y] == ‘*’){
count++;
z++;
if(str2[y+1] == str1[z]){
y++;
}
}
}
if(strlen(str1) == count)
return true;
else
return false;
}
}
int main(){
char str1[100],str2[100];
cout << "Enter string 1\n";
cin.getline(str1,100);
cout << "Enter string 2\n";
cin.getline(str2,20);
if(checkwildchar(str1,str2))
cout << "Yes\n";
else
cout << "NO\n";
return 0;
}