寻找单词中的字母模式

发布于 2025-01-02 04:33:58 字数 801 浏览 2 评论 0原文

有什么方法可以搜索“tro_ _ _e”,其中下划线代表缺失的字母?

我有一个文本文件,每行有 7 个字母的单词。例如,

trouble
control
reached
further
helping
shatter
biggest

我正在尝试将每个单词与字符串进行比较,

char check[10]="tro\0\0\0e"

此时我正在读取每一行并使用以下内容进行比较:

if(strstr(pword,check)!=NULL)
{
    fprintf(wfile,"%s\n",pword);
    }
}

fclose(file);

fclose(wfile);

我意识到我当前在 wfile: 中的输出

control
trouble

是由于“tro”和“tro”之间有三个 \0 “e”,所以比较只是在单词中找到“tro”。

有什么方法可以搜索“tro_ _ _e”,其中下划线代表缺失的字母?

这是针对绞刑吏游戏的,因此文件中的单词并不总是相同,并不总是 7 个字母长,并且模式并不总是“tro_ _ _e”,因为该模式代表玩家已经猜到的字母。本例中的答案:“trouble”

例如,如果玩家猜出了“r”、“u”和“l”。我会有一个字面上的字符串。

char check[10]="\0r\0u\0l\0\0\0\0\0" 因此我想要的搜索将是针对具有模式“_r_u_l”的任何单词

Is there any way that I can search for "tro_ _ _e" where the underscores represent missing letters?

I have a text file with a 7 letter word on each line. e.g

trouble
control
reached
further
helping
shatter
biggest

I am trying to compare each word to the string

char check[10]="tro\0\0\0e"

at the moment I am reading each line and comparing using:

if(strstr(pword,check)!=NULL)
{
    fprintf(wfile,"%s\n",pword);
    }
}

fclose(file);

fclose(wfile);

I realise that my current output in wfile:

control
trouble

is due to the fact that there are three \0s in between the "tro" and the "e" and so the comparison is just finding "tro" in the words.

Is there any way that I can search for "tro_ _ _e" where the underscores represent missing letters?

This is for a hangman game and so the words in the file are not always the same, not always 7 letters long and the pattern is not always "tro_ _ _e" as the pattern represents the letters already guessed by a player. The answer in this case: "trouble"

For example, if a player guessed "r", "u" and "l". I would have a string which was literally.

char check[10]="\0r\0u\0l\0\0\0\0\0" so the search I would want would be for any words with a pattern "_r_u_l"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

┼── 2025-01-09 04:33:58

如果您确实对 C# 解决方案持开放态度,正如您的标签所建议的:

string pword = "control\r\ntrouble\r\nreached\r\nfurther\r\nhelping\r\nshatter\r\nbiggest";
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("tro...e");
System.Text.RegularExpressions.MatchCollection mc = re.Matches(pword);
foreach (System.Text.RegularExpressions.Match m in mc)
{
   Console.WriteLine(m.Value);
}

If you are truly open to a C# solution, as suggested by your tags:

string pword = "control\r\ntrouble\r\nreached\r\nfurther\r\nhelping\r\nshatter\r\nbiggest";
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("tro...e");
System.Text.RegularExpressions.MatchCollection mc = re.Matches(pword);
foreach (System.Text.RegularExpressions.Match m in mc)
{
   Console.WriteLine(m.Value);
}
东京女 2025-01-09 04:33:58

我不完全确定您的问题实际上是什么,但我想您可以将两个搜索/条件与 AND 结合起来,例如:
if(strstr(pword,check) != NULL && pword[6]=='e') ...

I'm not entirely sure what your problem actually is but I guess you could combine two searches/conditions with AND, like:
if(strstr(pword,check) != NULL && pword[6]=='e') ...

久隐师 2025-01-09 04:33:58

不要搞乱 \0 - string.h 中的每个函数都认为它结束了字符串。使用_

也许你应该编写自己的函数。像这样的东西:

int isval(const char *word,const char *pat) {
 while (*word) {
  if (*pat!='_' && *pat!=*word) return 0;
  word++;
  pat++;
 }
 return !*pat;
}

Don't mess with \0 - Every function in string.h think that it ends strings. Use _.

Probably you should write your own function. something like:

int isval(const char *word,const char *pat) {
 while (*word) {
  if (*pat!='_' && *pat!=*word) return 0;
  word++;
  pat++;
 }
 return !*pat;
}
深爱成瘾 2025-01-09 04:33:58

如果您需要可扩展的解决方案,Posix 系统有、regcomp、regexec 等。

Posix systems have <regex.h>, with regcomp, regexec and so forth, if you need an extensible solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文