字符串匹配

/**************************************** 
字符串匹配问题:字符“*”可代表任意多个的任意字符。 
输入:一个不带“*”的字符串和一个通配字符串    
输出:如果匹配,则输出“Matched”;否则,输出“Not Matched” 
举例: 
例1:            例2:                 例3:             例4:              例5: 
abcdef           abcdef                abcdef            abcdef             abcdef  
abc*f            a**                   *f                 *d*f              *d 
Matched          Matched               Matched           Matched             Not Matched !
*****************************************/
#include "Match.h"
void StringMatch(char* srcString, char* matchString)
{
	int nSrcLen = strlen(srcString);
	int nCnt = 0;
	char* matchTemp = matchString;
	while(*srcString != 0)
	{
		if(*matchTemp == '*')//根据*代替需要匹配的字符
		{
			if(*(matchTemp+1) == '*')
			{
				++matchTemp;
			}
			if(*(matchTemp+1) != 0 && *srcString == *(matchTemp+1))
			{
				matchTemp += 2;

				if(*matchTemp == 0)
				{
					++nCnt;
					break;
				}
			}
			++srcString;
			if(*srcString == 0 && *(matchTemp+1) != 0)
			{
				break;
			}
			++nCnt;
		}else
		{
			if(*srcString == *matchTemp)
			{
				++matchTemp;
				if(*matchTemp == 0)
				{
					break;
				}
				++nCnt;
			}
			++srcString;
		}
	}
	if(nCnt == nSrcLen)
	{
		cout << "Matched" << endl;
	}else
	{
		cout << "Not Matched" << endl;
	}
}

你可能感兴趣的:(C++,c,F#,C#)