Click here to Skip to main content
Click here to Skip to main content
Go to top

Find a Word in a String

, 14 Mar 2014
Rate this:
Please Sign up or sign in to vote.
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

#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

/*=======*=========*=========*=========*=========*=========*=========*=========*
* 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)

Share

About the Author

mef526
Software Developer (Senior) Independant Software Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalVolynsky Alex23-Mar-14 10:07 
GeneralMy vote of 4 PinprofessionalVolynsky Alex23-Mar-14 10:07 
QuestionIt is a bug? Pinmembermaomx16-Mar-14 15:19 
AnswerRe: It is a bug? Pinmembermef52616-Mar-14 22:27 
Question[My vote of 1] Useless Pinmember.:floyd:.15-Mar-14 11:53 
AnswerRe: [My vote of 1] Useless Pinmembermef52616-Mar-14 0:05 
QuestionMessage Automatically Removed Pinmemberabo mahmoud14-Mar-14 15:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 14 Mar 2014
Article Copyright 2014 by mef526
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid