简体   繁体   English

比较两个字符串中的单词

[英]Compare words in two strings

I have made two strings. 我做了两个字符串。 User can fill them both. 用户可以填写它们。

char text[200];
char text2[200];  

I need to find similar words from both strings. 我需要从两个字符串中找到类似的单词。 For example, 例如,

Text= I am here for all my life 文字=我一生都在这里

Text2= They are here to win us all Text2 =他们在这里赢得了我们所有人

I need to program finds similar words like 'here','all'. 我需要编程找到类似'here','all'之类的单词。 I tried like this but it don't found all words. 我试过这样但却找不到所有的话。

if(strstr(text,text2) != NULL)

and then printf but i think it isnt the right thing. 然后是printf,但我觉得这不对。

I think this is what you want: 我想这就是你想要的:

char text[] = "I am here for all my life";
char text2[] = "They are here to win us all";

char *word = strtok(text, " ");

while (word != NULL) {
    if (strstr(text2, word)) {
        /* Match found */
        printf("Match: %s\n", word);
    }
    word = strtok(NULL, " ");
}

It uses strtok() to read the sentence word by word, and strstr() to search for the corresponding word in the other sentence. 它使用strtok()逐字逐句地读取句子,并使用strstr()来搜索另一句中的相应单词。 Note that this is not very efficient, if you have big chunks of data you'll have to consider a smarter algorithm. 请注意,这不是很有效,如果您有大量数据,则必须考虑使用更智能的算法。

UPDATE: 更新:

Since you don't want to match embedded words, strstr() is not of much help for you. 由于您不想匹配嵌入的单词,因此strstr()对您没有多大帮助。 Instead of using strstr() , you have to use a custom function. 而不是使用strstr() ,您必须使用自定义函数。 Something like this: 像这样的东西:

#include <ctype.h>
int searchword(char *text, char *word) {
    int i;

    while (*text != '\0') {
        while (isspace((unsigned char) *text))
            text++;
        for (i = 0; *text == word[i] && *text != '\0'; text++, i++);
        if ((isspace((unsigned char) *text) || *text == '\0') && word[i] == '\0')
            return 1;
        while (!isspace((unsigned char) *text) && *text != '\0')
            text++;
    }

    return 0;
}

The other code stays the same, but replace the call to strstr() by a call to this new function: 其他代码保持不变,但通过调用此新函数替换对strstr()的调用:

char text[] = "I am here for all my life";
char text2[] = "They are here to win us all";

char *word = strtok(text, " ");

while (word != NULL) {
    if (searchword(text2, word)) {
        /* Match found */
        printf("Match: %s\n", word);
    }
    word = strtok(NULL, " ");
}

You need to use combination of strtok() and strstr() . 您需要使用strtok()strstr()

split text into tokens with strtok() and search that token in text2 with strstr() 使用strtok()text拆分为标记,并使用strstr()text2搜索该标记

For safe Instead of strtok() You can also use strtok_r() 为了安全而不是strtok()你也可以使用strtok_r()

text分解为单词并使用strstrtext2搜索这些单词

There are two threads that I think would be helpful for you. 有两个线程我觉得对你有帮助。

How to extract words from a sentence efficiently in C? 如何在C中有效地从句子中提取单词?

Split string in C every white space. C中每个空格分割字符串。

Using strtok with a blank space as the delimiter seems like one appropriate solution to parse the two strings into words. 使用带空格的strtok作为分隔符似乎是将两个字符串解析为单词的合适解决方案。 It sounds like you've already implemented the second step (strsrt) effectively. 听起来你已经有效地实施了第二步(strsrt)。

Possible algorithm implementation: 可能的算法实现:

  • Get both strings from the user (Might be better to use char ** instead of char * ) 从用户获取两个字符串(可能更好地使用char **而不是char *
  • Sort each string using qsort 使用qsort对每个字符串进行排序
  • Start at the beginning of the smallest list of strings and begin your search 从最小的字符串列表的开头开始,然后开始搜索

Note: It is possible to have the last step execute in O(n) time 注意:可以在O(n)时间内执行最后一步

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM