題目:在一篇英文文章中查找指定的人名,人名使用二十六個英文字母(可以是大寫或小寫)、空格以及兩個通配符組成(*、?),通配符“*”表示零個或多個任意字母,通配符“?”表示一個任意字母。
如:“J* Smi??” 可以匹配“John Smith” .
請用C語言實現如下函數:
void scan(const char* pszText, const char* pszName);
注:pszText爲整個文章字符,pszName爲要求匹配的英文名。
請完成些函數實現輸出所有匹配的英文名,除了printf外,不能用第三方的庫函數等。
代碼:
#include "stdafx.h"
#include<stdio.h>
const char *p=NULL;
bool match(const char* ptext,const char* name) /* 字符串匹配函數,使用遞歸的調用方法*/
{
if(*name=='\0')
{
p=ptext;
return true;
}
if(*ptext=='\0')
{
if(*name=='*'&&*(name+1)=='\0')
{
p=ptext;
return true;
}
return false;
}
if(*name!='*'&&*name!='?')
{
if(*name==*ptext)
return match(ptext+1,name+1);
return false;
}
else
{
if(*name=='*')
return match(ptext+1,name)||match(ptext,name+1);
else
{
return match(ptext+1,name+1);
}
}
}
void scan(const char* ptext,const char* name)
{
while(*ptext!='\0')
{
if(*ptext==*name)
{
bool flag=match(ptext,name);
if(flag)
{
while(ptext!=p)
{
printf("%c",*ptext++);
}
printf("\n");
}
}
else
ptext++;
}
}
int main()
{
char ptext[100]={0};
char pname[100]={0};
gets(ptext);
gets(pname);
scan(ptext,pname);
return 0;
}