Check if all char values ​​are present in the string

I am currently working on this task and I am stuck. The goal is to read the file and find if these char values ​​exist in String from the file. I need to compare a String from a file with another string that I am inserting as an argument. However, as long as each char value is in a String from a file, it "matches".

Example (input and output):

./a.out file1 done
done in a suit
made not in a canine

Example (file1):

dunce
dog

As you can see, the order in which the lines are compared does not matter, and the file also follows one word per line. I put together a program that finds if the char value is on another line, but this is only part of the problem. Any idea how to do this?

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
    FILE *f = fopen(argv[1], "r");
    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    char *word = argv[2];

    if(argc != 3){
            printf("./a.out <file> <word>\n");
            exit(EXIT_SUCCESS);
    }

    if(f == NULL){
            printf("file empty\n");
            exit(EXIT_SUCCESS);
    }

    // confused what this loop does too
    while((read = getline(&line, &len, f)) != -1){
            char *c = line;
            while(*c){
                    if(strchr(word, *c))
                            printf("can't spell \"%s\" without \"%s\"!\n", line, word);
                    else
                            printf("no \"%s\" in \"%s\".\n", word, line);
            c++;
            }
    }
    fclose(f);
    exit(EXIT_SUCCESS);
}
+4
source share
2 answers

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 ) {    /* validate required arguments */
        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];  /* string holding search string */
    size_t slen = strlen(s), sum = 0, ulen;
    char uniq[slen+1];  /* unique characters in s */

    if (!fp) {  /* validate file open */
        fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
        return 1;
    }

    memset (uniq, 0, slen+1);  /* zero the VLA */
    /* fill uniq with unique characters from s */
    for (; *s; s++) if (!strchr (uniq, *s)) uniq[sum++] = *s;
    ulen = strlen (uniq);
    s = argv[2];    /* reset s */

    while (fgets (line, MAXC, fp)) {    /* for each line in file */
        if (strlen (line) - 1 < ulen) { /* short line, continue  */
            printf ("%s is not in %s", s, line);
            continue;
        }
        char *up = uniq;    /* ptr to uniq */
        sum = 0;            /* reset sum   */
        while (*up) if (strchr (line, *up++)) sum++; /* count chars */
        if (sum < ulen) /* validate sum */
            printf ("%s is not in %s", s, line);
        else
            printf ("%s is in %s", s, line);
    }
    fclose (fp); /* close file */

    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

, -, , .

, - .

0

confused what this loop does
while (read ..., , ,

* c line, c++, . while , * c (0).

if (strchr(word ... , . , , - , .

printf , , , "", , "" .

printf , . , , - . 1 0, , .

    /* set flag to 'letters all present' */
    int flag = 1;
    /* set pointer c to start of input line */
    c = word;
    /* test word from file for each letter in test word */
    while(*c) {
        if(strchr(line, *c) == NULL) {
            /* set flag to letter not present */
            flag = 0;
            break;
        }
        c++;
    }
0

All Articles