我目前正在完成这项任务,但被卡住了。目标是读取文件并查找文件中的字符串中是否存在这些字符值。我必须将文件中的字符串与我作为参数放入的另一个字符串进行比较。但是,只要每个字符值都在文件中的字符串中,它就会“匹配”。

示例(输入和输出):



示例(文件 1):



如您所见,比较字符串的顺序无关紧要,文件也每行跟随一个单词。我已经编写了一个程序,用于查找其他 String 中是否存在 char 值,但这只是问题的一部分。知道如何解决这个问题吗?

#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);
}

最佳答案

另一种方法是简单地保留从文件读取的行中匹配的每个字符的 sum,为提供给测试的单词中的每个唯一字符添加一个,如果总和等于由唯一字符组成的字符串的长度是搜索词,则搜索词中的每个唯一字符都包含在从文件读取的行中。

#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

您可以决定是否以不同方式处理重复字符,但您应该决定如何解决这种意外情况。

如果您有任何问题,请告诉我。

关于检查字符串中是否存在所有字符值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36572653/

10-10 23:08
查看更多