Find WHOLE word in a C string

Asked

Viewed 1,916 times

2

Hello.

I need to search a word in a text.

#include <stdio.h>
#include <string.h>

int main(void) {
    char *word = "dado"; // palavra a ser encontrada
    char *pageContent = "Ola tudo bem, vamos jogar dados"; // texto para procurar

    char* result = strstr(pageContent, word);

    printf("result: %s", result);
    return 0;
}

However, in this case it is returning "data". The text contains the word "DATA" but I want to find "DATA", only. How do I find a whole word without it being part of another word? Thank you.

  • 1

    The most general situation is the word to be between spaces. However, it can start at the first position or end at the last, with the corresponding space after or before. The other case you will need to consider is the score. Another thing is whether or not you will differentiate between upper and lower case letters.

  • Find or return ? In your example the search was supposed to fail because there is not only the word "given" or return only the part "given" ?

  • if it does not find the word it returns NULL, in my example I hope it is returned NULL, because the word 'given' does not exist only 'data'

  • Problem solved? If any response is appropriate you can accept it, or if no solution you can post your own.

3 answers

1


In C, the function strstr in C returns a pointer to the first occurrence of the word to be searched. That is, after the word is found, all characters after this substring will be included in the result.

For example, if char *word = "joga";, will be returned "jogar dados".

To get around this, you can use the substring length word.

#include <stdio.h>
#include <string.h>

int main(void) {
    int i;
    char *word = "dado";
    char *pageContent = "Ola tudo bem, vamos jogar dados";
    char* result = strstr(pageContent, word);

    printf("result: ");
    for (i = 0; i < strlen(word); i++)
        printf("%c", *(result + i));

    return 0;
}

0

#include <stdio.h>
#include <string.h>

int main()
{
    char str[] = "Ola tudo bem, vamos jogar dados";
    char search[] = "dado";
    char *ptr = strstr(str, search);

    if (ptr != NULL) /* Substring encontrada */
    {
        printf("'%s' contém'%s'\n", str, search);
    }
    else /* Substring não encontrada */
    {
        printf("'%s' não contém '%s'\n", str, search);
    }

    return 0;
}
  • hello friend, it didn’t work. The problem remains the same

0

A meneira to do is using Strtok(): http://www.cplusplus.com/reference/cstring/strtok/. One should only be careful with the use of this function because it modifies the string it takes as argument.

    char word[] = "dado"; // palavra a ser encontrada
    char pageContent[] = "dado ddado dadoo, .dado.?dado! !dados!"; // texto para procurar
    char *pt = NULL, str[100];
    strcpy(str, pageContent);
    pt = strtok(str, ",. !?"); // delimitadores que dividem as palavras
    int found=0;
    while( pt != NULL){
        if(!strcmp(word, pt)){
            printf("Found at position: %d\n", pt - str);
            found++;
        }
        pt = strtok(NULL, ",. !?");
    }   
    printf("We found %d ocurrences of the word: %s\n", found, word);

I believe that using other functions and fitting the need, it would be faster, but this way it seems more obvious.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.