4

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

I need to program finds similar words like '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.

Einārs Ozols
  • 155
  • 1
  • 10
  • Do you know what `strstr` does? – digital_revenant Oct 15 '13 at 17:51
  • 7
    The answer to this is not a fuction call but an algorithm you have to implement. So work on that: think about the problem and design a solution. – DSquare Oct 15 '13 at 17:54
  • If there was a single function to do this, the odds are it wouldn't have been assigned to you as a project/homework/whatever it is. Check out the [man page](http://linux.die.net/man/3/strstr) for `strstr` to understand why what you tried didn't work. There will be no occurrence of `text2`'s [full] content in `text` – Mike Oct 15 '13 at 18:09

5 Answers5

5

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. 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. Instead of using strstr(), you have to use a custom function. 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:

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, " ");
}
Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
1

You need to use combination of strtok() and strstr().

split text into tokens with strtok() and search that token in text2 with strstr()

For safe Instead of strtok() You can also use strtok_r()

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
1

Break down text into words and search for those words in text2 using strstr

digital_revenant
  • 3,274
  • 1
  • 15
  • 24
0

There are two threads that I think would be helpful for you.

How to extract words from a sentence efficiently in C?

Split string in C every white space.

Using strtok with a blank space as the delimiter seems like one appropriate solution to parse the two strings into words. It sounds like you've already implemented the second step (strsrt) effectively.

Community
  • 1
  • 1
Mish
  • 31
  • 6
0

Possible algorithm implementation:

  • Get both strings from the user (Might be better to use char ** instead of char *)
  • Sort each string using 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

smac89
  • 39,374
  • 15
  • 132
  • 179