Compare words in two lines

I made two lines. User can fill both of them.

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

I need to find similar words from both lines. For example,

Text = I've been here all my life

Text2 = They are here to win us all

I need to program find similar words, such as "here", "everything." I tried so, but did not find all the words.

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

and then printf, but I think it is not.

+4
source share
5 answers

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, " ");
}

strtok() strstr() . , , , .

UPDATE:

, strstr() . strstr() . - :

#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;
}

, 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, " ");
}
+5

strtok() strstr().

text strtok() text2 strstr()

strtok() strtok_r()

+1

text text2, strstr

+1

, , , .

C?

C .

strtok , . , (strsrt) .

0

:

  • Get both lines from the user (better to use char **instead char *)
  • Sort each row with qsort
  • Start at the beginning of the smallest list of lines and start your search.

Note. Perhaps the last step in O(n)time

0
source

All Articles