我正在尝试编写一个比较两个字符串的程序。如果 *s1
和 *s2
相同,我想匹配它们。但是,如果 s2
包含一个星号 ('*')
,我们可以用任何字符串(甚至是空字符串)替换这个星号来生成 s1
并且s2
相同,s2
可以容纳尽可能多的星星。
例如,"main.c"
和 "*.c"
可以匹配,因为可以将 '*'
替换为字符串 "main"
使这两个字符串相同。到目前为止,我的尝试可以在下面的代码中看到:
#include
int identic(char *s1, char *s2)
{
if(*s1 != '\0' && s2 == '')
return (identic(s1 + 1, s2) || identic(s1, s2 + 1));
if(*s1 == '\0' && s2 == '')
return (identic(s1, s2 + 1));
if(*s1 == *s2 && *s2 != '\0' && *s2 != '\0')
return (identic(s1 + 1, s2 + 1));
if(*s1 == *s2 && *s1 == '\0' && *s2 == '\0')
return (1);
return (0);
}
int main() {
printf("%c\n", identic("asfd", "f"));
}
最佳答案
当我替换 s2 == ''
时与 *s2 == '*'
和 #include
与 #include <stdio.h>
,您的代码似乎工作正常。
关于c - 制作一个匹配两个字符串(如果它们相同)的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38421298/