比较通配符字符串

编程入门 行业动态 更新时间:2024-10-25 11:33:56
本文介绍了比较通配符字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在C,我究竟可以比较包含字符串* (即可能是字符的任意组合)与字符串的二维矩阵?

例如,我有字去* S 。它可以生成的话华丽( * 是orgeou),善,山羊,云等我应该从读的话整个字典,并与包含一个或多个星号字加以比较( * )。可以从与星号的字中产生的每个字将不得不被打印。如果这两个词具有相同的长度那么很容易比较,因为 * 只能是一个字母。

INT FQ(字符* S1,字符* S2){INT I,A = 0,B = 0,S = 0;而(1){    如果(S1 [A] =='\\ 0')        打破;    一个++;}如果(strlen的(S1)== strlen的(S2)){    对于(i = 0; I< A;我++){        如果(S1 [Ⅰ] ==*){            基础B ++;            }        如果(S1 [Ⅰ] == S2 [I]){            基础B ++;            }        }    }如果(二==一)    返回1;

解决方案

您可以很容易写一个递归函数通过检查字符模式字符串中的字符与通配符中有一个刺比较另一个字符串,并应用这些规则如下:

  • 如果模式[P] =='\\ 0':模式匹配,如果候选人[C] =='\\ 0'
  • 如果模式[P] =='*':尝试匹配的候选人[C] ...候选人[C + N]与图案[P + 1]
  • 如果模式[P]!='?'和模式[P] =候选[C]:暂无匹配
  • 否则,匹配模式[P + 1]的候选[C + 1]

这几个规则很容易被写成进行匹配的递归函数:

的#include< stdbool.h>布尔匹配(为const char *模式,为const char *候选人,INT P,INT C){  如果(模式[P] =='\\ 0'){    返回候选[C] =='\\ 0';  }否则如果(图案[P] ==*){    对(候选[C] ='\\ 0';!C ++){      如果(匹配(模式,候选人,P + 1,C))        返回true;    }    返回匹配(模式,候选人,P + 1,C);  }否则如果(图案[P] =&放大器!'?';&放大器;!图案[P] =候补并[c]){    返回false;  }其他{    返回匹配(模式,候选人,P + 1,C + 1);  }}

那么,你可以这样做:

匹配(F * O,富,0,0);

这是不是一种有效的方法,但我认为这是很容易理解和执行。 en.wikipedia/wiki/:如果你需要的东西更有效,你可以从这些开始String_searching_algorithm

In C, how exactly can I compare a string that contains * (that can be any combination of characters) with a two dimensional matrix of strings?

For example, I have the word go*s. It can generate the words "gorgeous" (* is "orgeou"), "goodness", "goats", "goes" etc. I am supposed to read the words from a whole dictionary and compare them with a word that contains one or more asterisks (*). Each word that can be generated from the word with the asterisk will have to be printed. If the two words have the same length then it is easy to compare because the * can only be one letter.

int fq(char *s1, char *s2){ int i, a=0, b=0, s=0; while (1){ if (s1[a]=='\0') break; a++; } if (strlen(s1)==strlen(s2)){ for(i=0; i<a; i++){ if (s1[i]=='*'){ b++; } if (s1[i]==s2[i]){ b++; } } } if (b==a) return 1;

解决方案

You can fairly easily write a recursive function for comparing a sting to another string with a wildcard in it by examining the pattern string character by character and applying these rules as follows:

  • if pattern[p] == '\0': the patterns match if candidate[c] == '\0'
  • if pattern[p] == '*': try to match candidate[c]...candidate[c+n] with pattern[p+1]
  • if pattern[p] != '?' and pattern[p] != candidate[c]: No match
  • otherwise, match pattern[p+1] with candidate[c+1]

These few rules can easily be written as a recursive function for matching:

#include <stdbool.h> bool match(const char *pattern, const char *candidate, int p, int c) { if (pattern[p] == '\0') { return candidate[c] == '\0'; } else if (pattern[p] == '*') { for (; candidate[c] != '\0'; c++) { if (match(pattern, candidate, p+1, c)) return true; } return match(pattern, candidate, p+1, c); } else if (pattern[p] != '?' && pattern[p] != candidate[c]) { return false; } else { return match(pattern, candidate, p+1, c+1); } }

then, you can do:

match("f*o", "foo", 0, 0);

This is not an effective method, but I think it is easy to understand and implement. If you need something more efficient, you can start from these: en.wikipedia/wiki/String_searching_algorithm

更多推荐

比较通配符字符串

本文发布于:2023-10-24 14:53:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524232.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!