检查字符串中是否存在所有字符值 [英] Check if all char values are present in string

查看:117
本文介绍了检查字符串中是否存在所有字符值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理此作业,我被卡住了。目标是读取一个文件,并找到这些字符值是否存在于文件的字符串中。我必须比较一个字符串从文件到另一个字符串我放入作为参数。但是,只要每个字符值在文件的字符串中,则它匹配。

I'm currently working on this assignment and I'm stuck. The objective is to read a file and find if these char values exist in the String from the file. I have to compare a String from a file to another String I put in as an argument. However, just as long as each char value is in the String from the file then it "matches".

示例(输入和输出):


./ a.out file1doneÁ
done在骨头中
done不在doggie


./a.out file1 done
done is in bonehead
done is not in doggie

示例(file1):




doggie

bonehead
doggie

正如你可以看到的顺序是比较字符串并不重要,每行一个字。我把一个程序,发现如果char值存在于另一个字符串,但这只是问题的一部分。有任何想法如何去做这个?

As you can see the order in which is compares Strings does not matter and the file also follows one word per line. I've put together a program that finds if the char value is present in the other String but that is only part of the problem. Any idea how to go about 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);
}


推荐答案

sum 在从文件中读取的行中匹配的每个字符,为提供给test的单词中的每个唯一字符添加一个字符,如果和等于由唯一字符组成的字符串是搜索项,则搜索项中的每个唯一字符都包含在从文件读取的行中。

Another approach would simply keep a sum of each character matched in the line read from the file, adding one for each unique character in the word supplied to test, and if the sum is equal to the length of the string made up by the unique characters is the search term, then each of the unique characters in the search term 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

这对搜索字符串中的重复字符同样有效。例如

which would work equally well for duplicate characters in the search string. e.g.

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

您可以决定是否以不同的方式处理重复字符,但是您应该做出一些决定

You can decide if you would handle duplicate characters differently, but you should make some determination on how that contingency will be addressed.

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

Let me know if you have any questions.

这篇关于检查字符串中是否存在所有字符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆