It is possible to make it more general using regular expressions with the regex() service. Here is an example program with a function named find_pattern() accepting two parameters: the string into which the search is to be done and a pattern to look for.
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
#include <libgen.h>
const char *find_pattern(const char *string, const char *pattern)
{
int rc;
regex_t reg;
const char *found = (const char *)0;
regmatch_t match;
rc = regcomp(®, pattern, REG_EXTENDED|REG_NEWLINE);
if (rc != 0) {
fprintf(stderr, "Invalid regex '%s', error %d\n", pattern, rc);
return (const char *)0;
}
rc = regexec(®, string, 1, &match, 0);
if (0 == rc) {
found = string + match.rm_so;
}
regfree(®);
return found;
} // find_pattern
int main(int ac, char *av[])
{
const char *str;
if (ac != 3) {
fprintf(stderr, "Usage: %s string regex\n", basename(av[0]));
return 1;
}
str = find_pattern(av[1], av[2]);
if (str) {
printf("%s\n", str);
return 0;
}
return 1;
} // main
Some tries:
$ gcc search.c -o search
$ ./search "azerty wOrD qwErty" '[wW][Oo][rR][dD]'
wOrD qwErty
$ ./search "azerty word qwErty" '[wW][Oo][rR][dD]'
word qwErty
$ ./search "azerty word qwErty" '[wW][Oo][R][D]'
$ echo $?
1
$ ./search "azerty woRD qwErty" '[wW][Oo][R][D]'
woRD qwErty