Another approach would simply save sumeach character matched in the line read from the file, adding one for each unique character in the word provided for testing, and if the sum is equal to the length of the string made by the unique characters is a search term, then each of the unique characters in the search expression are included in the line read from the file.
#include <stdio.h>
#include <string.h>
#define MAXC 256
int main (int argc, char **argv) {
if (argc < 3 ) {
fprintf (stderr, "error: insufficient input, usage: %s file string\n",
argv[0]);
return 1;
}
FILE *fp = fopen (argv[1], "r");
char line[MAXC] = "";
char *s = argv[2];
size_t slen = strlen(s), sum = 0, ulen;
char uniq[slen+1];
if (!fp) {
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
memset (uniq, 0, slen+1);
for (; *s; s++) if (!strchr (uniq, *s)) uniq[sum++] = *s;
ulen = strlen (uniq);
s = argv[2];
while (fgets (line, MAXC, fp)) {
if (strlen (line) - 1 < ulen) {
printf ("%s is not in %s", s, line);
continue;
}
char *up = uniq;
sum = 0;
while (*up) if (strchr (line, *up++)) sum++;
if (sum < ulen)
printf ("%s is not in %s", s, line);
else
printf ("%s is in %s", s, line);
}
fclose (fp);
return 0;
}
/
$ ./bin/strallcinc dat/words.txt done
done is in bonehead
done is not in doggie
. .
$ ./bin/strallcinc dat/words.txt doneddd
doneddd is in bonehead
doneddd is not in doggie
, -, , .
, - .