我想看一个字符串(含有?*,与文曲星类似)与另一个普通字符串是否匹配,
如cl*k 与 clock 是匹配的。
有好的算法吗?
或者现成的函数,最好给个例子
谢谢。
关于正则表达式,个人感觉过于复杂繁琐,如果可以给个例子也很感谢:)
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<string.h>
main()
{
char *p1=NULL,str[10]={'\0'},*p2=NULL,str2[10]={'\0'};
int i,flag=0;
printf("the first string:");
gets(str);
printf("the second string:");
gets(str2);
if(strlen(str)-strlen(str2))
flag=1;
else
{
p1=str;
p2=str2;
for(;*p1!='\0';p1++,p2++)
{
if(*p1==*p2)
continue;
else
if(*p2=='*'||*p2=='?')
continue;
else
{
flag=1;
break;
}
}
}
if(flag==0)
printf("the two string accord with each other!");
else
printf("the two string do not accord with each other!");
getch();
}
这个算法比较直观.
----------------解决方案--------------------------------------------------------
一个*可能对应多个字符啊
----------------解决方案--------------------------------------------------------
用堆栈
----------------解决方案--------------------------------------------------------
啊,楼主不好意思,我看差了你的意思,等会我再想下
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<string.h>
main()
{
char *p1=NULL,str[10]={'\0'},*p2=NULL,str2[10]={'\0'};
int i,flag=0;
printf("the first string:");
gets(str);
printf("the second string:");
gets(str2);
p1=str;
p2=str2;
for(;*p1!='\0'&&*p2!='*'&&*p2!='?';p1++,p2++)
{
if(*p1==*p2)
continue;
else
{
flag=1;
break;
}
}
p1=str+strlen(str)-1;
p2=str2+strlen(str2)-1;
for(;flag==0&&*p2!='*'&&*p2!='?';p1--,p2--)
{
if(*p1==*p2)
continue;
else
{
flag=1;
break;
}
}
if(flag==0)
printf("the two string accord with each other!");
else
printf("the two string do not accord with each other!");
getch();
}
呵,这样就行了,从后面再检验一次就行了.
----------------解决方案--------------------------------------------------------
谢谢啦啊
----------------解决方案--------------------------------------------------------
其实还是不太行的,
比如:
abcdefghi
ab*k*hi
这不算匹配吧?
可我这程序还是算出结果是匹配的答案来.
这个程序只用于*只有一个啦,呵呵,不好意思。
----------------解决方案--------------------------------------------------------