Click here to Skip to main content
12,629,978 members (32,596 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.8K views
11 bookmarked
Posted

Find a Word in a String

, 14 Mar 2014 CPOL
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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Volynsky Alex23-Mar-14 11:07
professionalVolynsky Alex23-Mar-14 11:07 
GeneralMy vote of 4 Pin
Volynsky Alex23-Mar-14 11:07
professionalVolynsky Alex23-Mar-14 11:07 
QuestionIt is a bug? Pin
maomx16-Mar-14 16:19
membermaomx16-Mar-14 16:19 
AnswerRe: It is a bug? Pin
mef52616-Mar-14 23:27
membermef52616-Mar-14 23:27 
Question[My vote of 1] Useless Pin
.:floyd:.15-Mar-14 12:53
member.:floyd:.15-Mar-14 12:53 
AnswerRe: [My vote of 1] Useless Pin
mef52616-Mar-14 1:05
membermef52616-Mar-14 1:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 14 Mar 2014
Article Copyright 2014 by mef526
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid