Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 检查字符串中是否存在所有字符值_C_String_File_Char_Substring - Fatal编程技术网

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

C 检查字符串中是否存在所有字符值,c,string,file,char,substring,C,String,File,Char,Substring,我现在正在做这个任务,我被卡住了。目标是读取一个文件,并查找文件中的字符串中是否存在这些字符值。我必须将文件中的一个字符串与作为参数输入的另一个字符串进行比较。但是,只要文件中的字符串中包含每个char值,它就会“匹配” 示例(输入和输出): ./a.out文件1已完成 完成是愚蠢的 狗狗不吃狗肉 示例(文件1): 骨头头 小狗 正如您所看到的,is比较字符串的顺序并不重要,文件每行也跟随一个单词。我已经编写了一个程序,可以找到另一个字符串中是否存在char值,但这只是问题的一部分。你知道怎么做

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

示例(输入和输出):

./a.out文件1已完成
完成是愚蠢的
狗狗不吃狗肉

示例(文件1):

骨头头
小狗

正如您所看到的,is比较字符串的顺序并不重要,文件每行也跟随一个单词。我已经编写了一个程序,可以找到另一个字符串中是否存在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);
}
定义GNU源
#包括
#包括
#包括
int main(int argc,字符**argv){
文件*f=fopen(argv[1],“r”);
char*line=NULL;
尺寸长度=0;
阅读;
char*word=argv[2];
如果(argc!=3){
printf(“./a.out\n”);
退出(退出成功);
}
如果(f==NULL){
printf(“文件空\n”);
退出(退出成功);
}
//我也不明白这个循环的作用
while((read=getline(&line,&len,f))!=-1){
char*c=行;
而(*c){
if(strchr(字,*c))
printf(“如果没有\%s\”!\n、行、字,就不能拼写\%s\);
其他的
printf(“在\%s\”中没有\%s\”\n,字,行);
C++;
}
}
fclose(f);
退出(退出成功);
}

混淆了这个循环的作用

while(read…
行显然从文件中读入行,并将它们放在line变量中

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

if(strhr(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++;
    }

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

#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 doneddd
doneddd is in bonehead
doneddd is not in doggie
您可以决定是否以不同的方式处理重复字符,但您应该确定如何处理意外情况


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

我似乎收到了一些意见,但没有得到任何回应,我的问题措辞是否糟糕?如果需要,我可以重写或以不同方式解释。作业是检查字符,而不是字符串。因此,根据您编写的要求,字符顺序并不重要(老师可能会给出不同的指示)。只是让人困惑。在这里,您正在测试文件中的字符是否在您的输入中。只需将变量
all\u found
设置为真值;如果
strchr
返回
NULL
,将
all\u found
设置为假值并中断。啊,您应该使用
strchr
char*c=word;
(第行,*c)
;您正在检查命令行中的所有字符是否都存在于给定行中。
$ ./bin/strallcinc dat/words.txt doneddd
doneddd is in bonehead
doneddd is not in doggie