2

テキストファイルを1行ずつ読み上げようとしています

FILE *infile;
char line[1000];
infile = fopen("file.txt","r");
while(fgets(line,1000,infile) != NULL) 
{
    //....
}
fclose(infile);

次に、「the」などの特定の単語を見つける必要があり、それが何回発生し、どの行で発生するかを確認する必要があります。

これで単語数が数えられるはず

int wordTimes = 0;
if((strcmp("the", currentWord) == 0)) 
{
    printf("'%s' appears in line %d  which is: \n%s\n\n", "the", line_num, line);
    wordTimes++;
}

ここlineで、 は文字列が出現するテキスト行であり、line_numはそれが出現する行番号です。

そして、単語が表示される回数は次のコードを使用します。

if(wordTimes > 0)
{
    printf("'%s' appears %d times\n", "the", wordTimes);
}
else
{
    printf("'%s' does not appear\n", "the");
}

問題は、行の各単語を「the」と比較して、それが適用される行を出力する方法がわからないことです。

これには非常に基本的な C を使用する必要があるため、strtok()orは使用できませんstrstr()strlen()としか使えませんstrcmp()

4

1 に答える 1

4

strword()たぶん、このような関数を書く必要があります。の分類関数 (マクロ) を使用できると想定していますが<ctype.h>、それも許可されていない場合の回避策があります。

#include <assert.h>
#include <ctype.h>
#include <stdio.h>

char *strword(char *haystack, char *needle);

char *strword(char *haystack, char *needle)
{
    char *pos = haystack;
    char old_ch = ' ';
    while (*pos != '\0')
    {
        if (!isalpha(old_ch) && *pos == *needle)
        {
            char *txt = pos + 1;
            char *str = needle + 1;
            while (*txt == *str)
            {
                if (*str == '\0')
                    return pos;     // Exact match at end of haystack
                txt++, str++;
            }
            if (*str == '\0' && !isalpha(*txt))
                return pos;
        }
        old_ch = *pos++;
    }
    return 0;
}

int main(void)
{
    /*
    ** Note that 'the' appears in the haystack as a prefix to a word,
    ** wholly contained in a word, and at the end of a word - and is not
    ** counted in any of those places. And punctuation is OK.
    */
    char haystack[] =
        "the way to blithely count the occurrences (tithe)"
        " of 'the' in their line is the";
    char needle[] = "the";

    char *curpos = haystack;
    char *word;
    int count = 0;
    while ((word = strword(curpos, needle)) != 0)
    {
        count++;
        printf("Found <%s> at [%.20s]\n", needle, word);
        curpos = word + 1;
    }

    printf("Found %d occurrences of <%s> in [%s]\n", count, needle, haystack);

    assert(strword("the", "the") != 0);
    assert(strword("th", "the") == 0);
    assert(strword("t", "t") != 0);
    assert(strword("", "t") == 0);
    assert(strword("if t fi", "t") != 0);
    assert(strword("if t fi", "") == 0);
    return 0;
}

実行すると、次のものが生成されます。

Found <the> at [the way to blithely ]
Found <the> at [the occurrences (tit]
Found <the> at [the' in their line i]
Found <the> at [the]
Found 4 occurrences of <the> in [the way to blithely count the occurrences (tithe) of 'the' in their line is the]

strwordなしで機能を実行する方法はあり<ctype.h>ますか?

はい。私は冒頭の段落で同じことを言いました。使用される唯一の関数/マクロはであるため、(EBCDIC を使用するシステム上にない) ラテン アルファベットが連続するようにいくつかの仮定isalpha()を行うことができます。ヘッダー:is_alpha()isalpha()<ctype.h>

static inline int is_alpha(int c)
{
    return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
于 2015-05-23T05:19:10.400 に答える