检查字符串中是否存在所有char值

时间:2016-04-12 12:00:14

标签: c string file char substring

我正在进行这项任务,而且我被困住了。目标是读取文件并查找文件中String中是否存在这些char值。我必须将文件中的String与作为参数放入的另一个String进行比较。但是,只要每个char值都在文件的String中,那么它就“匹配”。

示例(输入和输出):

  

./ a.out file1完成
  完成是在笨蛋中   完成不是在小狗

示例(file1):

  


  狗

正如您所看到的那样,比较字符串无关紧要,文件每行也跟着一个单词。我把一个程序放在一起,查找char值是否存在于另一个String中,但这只是问题的一部分。知道如何去做吗?

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

2 个答案:

答案 0 :(得分:0)

confused what this loop does
while (read ...行显然会从文件中读取行,将它们放在行变量

* c是指向变量line的开头的指针,并且此指针递增c++,以便访问文件中单词中的每个字母。当* c指向空终止符(0)时,while循环将终止。

if (strchr(word ...行正在测试测试词是否包含文件中单词的一个字母。 这似乎与您尝试做的相反 - 查找测试单词中的所有字母是否都可以在文件中的单词中找到。

printf行是不合理的,因为没有/或 - 你需要一行打印&#39;是&#39;我们的信件存在,一行打印&#39; no&#39;至少有一封信不存在。

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++;
    }

答案 1 :(得分:0)

另一种方法是简单地保留从文件读取的行中匹配的每个字符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

对搜索字符串中的重复字符同样有效。 e.g。

$ ./bin/strallcinc dat/words.txt doneddd
doneddd is in bonehead
doneddd is not in doggie

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

如果您有任何问题,请与我们联系。