Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C

Find a Word in a String

4.83/5 (7 votes)
14 Mar 2014CPOL 17.6K  
Finding a substring is trivial, but is it a word?

Introduction

There is no C library function to find the first occurrence of a word in a string. When using strstr(), a code defect occurs when the match finds a value that is not in a complete word.

Using the Code

Link with shlwapi.lib to get StrStrI():

In header

C++
#define IN 
#define OPTIONAL
#include <windows.h>
#include <shlwapi.h>

char * StrStrWord 
    ( IN char *pcSearched
    , IN const char *pcWordToFind
    , IN const size_t nSearchedSize
    , OPTIONAL IN int bUseCase = 1
    , OPTIONAL IN char *pcWordChars = NULL);   

In C++ module

C++
/*=======*=========*=========*=========*=========*=========*=========*=========*
* FUNCTION: StrStrWord
* -----------------------------------------------------------------------------
* \brief
*    Point to the word in the search string
*
* PARAMETERS:
*    pcSearched: IN. The string to search
*    pcWordToFind: IN. The string to find
*    nSearchedSize: IN. The sizeof of the searched string
*    bUseCase: IN. Flag for case insensitive search
*    pcWordChars. IN. A list of valid chars in a word
*
* RETURN VALUE: Pointer to location of word in the search string
*    or NULL if string not found
*--------+---------+---------+---------+---------+---------+---------+--------*/
char * StrStrWord
    (IN char *pcSearched
    , IN const char *pcWordToFind
    , IN const size_t nSearchedSize
    , OPTIONAL IN int bUseCase // = 1
    , OPTIONAL IN char *pcWordChars // = NULL
) {
    static const char acWord[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
    if (!pcSearched || !pcWordToFind) 
        return NULL;
    size_t nLenSearchStr = strnlen(pcWordToFind, nSearchedSize);
    size_t nLenWordStr = strnlen(pcWordToFind, nSearchedSize); 
    if (nSearchedSize < nLenSearchStr)
        return NULL;
     const char *pcCharsInWord;
    if (pcWordChars == NULL)
        pcCharsInWord = acWord;
    else
        pcCharsInWord = pcWordChars;
     if (bUseCase) {
        for (char *pc = strstr(pcSearched, pcWordToFind)
            ; pc
            ; pc = strstr(pc + 1, pcWordToFind))
        {
            const char *pcSepEnd = strchr(pcCharsInWord, pc[nLenWordStr]);
            if (!pcSepEnd) {
                if (pc == pcSearched)
                    return pc;
                const char *pcSepStart = strchr(pcCharsInWord, pc[-1]);
                if (!pcSepStart)
                    return pc;
            }
        }
    }
    else {
        for (char *pc = StrStrI(pcSearched, pcWordToFind)
            ; pc
            ; pc = StrStrI(pc + 1, pcWordToFind))
        {
            const char *pcSepEnd = strchr(pcCharsInWord, pc[nLenWordStr]);
            if (!pcSepEnd) {
                if (pc == pcSearched)
                    return pc;
                const char *pcSepStart = strchr(pcCharsInWord, pc[-1]);
                if (!pcSepStart)
                    return pc;<br />            }
        }
    }
    return NULL;
} // StrStrWord()  

History

  • 14th March, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)