In order to determine if the word you are searching for within a string exists as a whole word and not part of a substring of another word within the string, you simply need to located the substring (whether a whole word or not) and then check if the character before and after is [A-Za-z0-9] (e.g. isalnum() -- adjust the test as needed).
If the character before or after your substring is an alpha-character or digit, then your search word does not exist as a whole-word within the string.
If, on the other had, you find the character that comes before (if any) your substring, and the character that comes after (if any) your substring is whitespace of punctuation -- then you can consider the word a whole word within the string.
You can do that by simply checking the length of your search term, locating your search substring within the string and then using pointer arithmetic (or array indexing) to isolate the character before and after your substring (making sure you test whether you search word begins at the beginning or end of your string as well).
A simple function could be:
const char *findword (const char *s, const char *w)
{
size_t len = strlen (w); /* get length of word */
char *sp = strstr (s, w); /* get start-pointer to word in string */
if (!sp) /* if start-pointer NULL - not found */
return NULL;
if (sp > s && isalnum (*(sp-1))) /* if not 1st char and char before sp */
return NULL; /* is [A-Za-z0-9], not a whole word */
if (sp[len] && isalnum(sp[len])) /* if end not nul-terminating char, and */
return NULL; /* and char after w is [A-Za-z0-9], not
a whole word */
return sp; /* return pointer to whole word */
}
(note: if you attempt to use strtok() or strsep() both modify the search string, so you must ensure it is mutable)
The function returns a pointer to the beginning of the whole word match within your string on success, NULL otherwise. You can adjust the search to limit what characters are considered valid at each end of the word.
A short program using the function and taking the sentence to search as the first argument and the word to search for as the second (or using "my cat likes hotdogs" and "dog" for defaults if no, or only the sentence is given), you could do:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
const char *findword (const char *s, const char *w)
{
size_t len = strlen (w); /* get length of word */
char *sp = strstr (s, w); /* get start-pointer to word in string */
if (!sp) /* if start-pointer NULL - not found */
return NULL;
if (sp > s && isalnum (*(sp-1))) /* if not 1st char and char before sp */
return NULL; /* is [A-Za-z0-9], not a whole word */
if (sp[len] && isalnum(sp[len])) /* if end not nul-terminating char, and */
return NULL; /* and char after w is [A-Za-z0-9], not
a whole word */
return sp; /* return pointer to whole word */
}
int main (int argc, char **argv) {
char *text = argc > 1 ? argv[1] : "my cat likes hotdogs",
*word = argc > 2 ? argv[2] : "dog";
printf ("text : %s\nword : %s\n%s\n",
text, word, findword (text, word) ? "FOUND" : "NOT FOUND");
}
(the program above just uses the return to determine whether to output "FOUND" on successfully finding word as a whole-word within the string, or "NOT FOUND" on failure)
Example Use/Output
Default case looking for "dog" as whole word:
$ ./bin/findwholeword
text : my cat likes hotdogs
word : dog
NOT FOUND
Looking for whole word "cat":
$ ./bin/findwholeword "my cat likes hotdogs" "cat"
text : my cat likes hotdogs
word : cat
FOUND
Looking for whole word "like":
$ ./bin/findwholeword "my cat likes hotdogs" "like"
text : my cat likes hotdogs
word : like
NOT FOUND
Looking for whole word "likes":
$ ./bin/findwholeword "my cat likes hotdogs" "likes"
text : my cat likes hotdogs
word : likes
FOUND
Let me know if you have any further questions.