【字符串】字符串通配符

字符串通配符 

描述: 

问题描述:在计算机中,通配符一种特殊语法,广泛应用于文件搜索、数据库、正则表达式等领域。现要求各位实现字符串通配符的算法。
要求:
实现如下2个通配符:
*:匹配0个或以上的字符(字符由英文字母和数字0-9组成,不区分大小写。下同)
?:匹配1个字符


输入:
通配符表达式;
一组字符串。


输出:
返回匹配的结果,正确输出true,错误输出false

 
知识点:  字符串 
题目来源:  内部整理 
练习阶段:  初级 
运行时间限制: 10Sec
内存限制: 128MByte
输入:  

先输入一个带有通配符的字符串,再输入一个需要匹配的字符串

 
输出:  

返回匹配的结果,正确输出true,错误输出false

 
样例输入:
te?t*.*
txt12.xls
                   
样例输出:
false


#include<iostream>
#include<string>
#include<cctype>
using namespace std;
bool Match(string match_str, string str)
{
	int i=0;
	int j=0;
	bool is_match=true;
	for (i =0,j=0; i <match_str.length()&&j<str.length(); i++,j++)
	{
		if (match_str[i]!='?'&&match_str[i]!='*')
		{
			if (match_str[i]!=str[i])
			{
				is_match=false;
				break;
			}
		}
		else if (match_str[i]=='?')
		{
			if(isalnum(str[i]))
				continue;
			else
				is_match=false;
				break;
		}
		else if (match_str[i]=='*')
		{
			while (i < match_str.length())  
				{  
					if (match_str[i] == '*' || match_str[i] == '?')  
						i++;  
					else  
						break;  
				}  
  
			if (match_str[match_str.length()-1] == '*')  
			   {  
				   is_match = true;  
				   break;  
               }  

			while (j < str.length() && isalnum(str[j]))  
			   {  
				   if (match_str[i] != str[j])  
					   j++;  
				   else  
					  break;  
			   }  
  
           if (str.length() == j ||false == isalnum(str[j]))  
			   {  
				   is_match = false;  
				   break;  
			   }   
           if(j+1 == str.length() && i+1 != match_str.length() )  
			   {  
				   is_match = false;  
				   break;  
			   }   
  
		}
	}
   return is_match;  
}

int main()
{
	string match_str;
	string str;
	cin>>match_str>>str;
	for (int i = 0; i < match_str.length(); i++)
	{
		match_str[i]=tolower(match_str[i]);
	}
	for (int j = 0; j < str.length(); j++)
	{
		str[j]=tolower(str[j]);
	}
	if (Match(match_str,str))
		cout<<"true"<<endl;
	else
		cout<<"false"<<endl;
	return 0;
}
















评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值