C语言---单词检查

已知有一个正确单词索引表(保存在当前目录下的文件index.txt中,且全为小写字母,按照字典序由小到大排列,每个单词独占一行),编写程序利用该单词表对某一英文文章(保存在当前目录下的另一个文件in.txt中)进行单词正确性检查,若该英文文章中出现的单词(只有连续字母组成)没有出现在单词索引文件中(检查时大小写无关),则将该出错的单词(其中的字母全部转换为小写)输出到当前目录下的另一文件error.txt中,每个单词独占一行,并且以字典序由小到大的顺序输出。
假设:
1、in.txt中的文章有可能没有经过排版,格式有可能杂乱无章,也有可能没有写完整。
2、index.txt中的单词个数不超过1000个,每个单词的长度不超过50个字母。
3、若出错的单词多次出现,则多次输出。

【输入形式】

保存单词索引表的文件index.txt和保存英文文章的文件in.txt都位于当前目录下。

【输出形式】

将出错的单词以字典序由小到大的顺序输出到当前目录下的文件error.txt中,每个单词单独占一行,多次出错的单词多次输出。若没有出现错误单词,则什么也不输出。

【样例输入1】

假设文件in.txt内容为:

There are two verrsions of the international standards for C.
Thee first version was ratified in 1989 by the American National
Standards Institue (ANS1) C standard committee.It is often
referred as ANS1 C or C89. The secand C standard was completed
in 1999. This standard is comonly referred to as C99. C99 is a
milestone in C’s evolution into a viable programing languga
for numerical and scientific computing.

文件index.txt中的单词索引表内容为:

a
american
and
ansi
are
as
by
c
committee
commonly
completed
computing
evolution
first
for
in
institue
international
into
is
it
language
milestone
national
numerical
of
often
or
programming
ratified
referred
s
scientific
secand
standard
standards
the
there
this
to
two
version
versions
viable
was

【样例输出1】

文件error.txt中出错的单词应为:

ans
ans
comonly
languga
programing
thee
verrsions

【样例1说明】

用index.txt中的单词索引表对in.txt中出现的每一个单词进行检查,检查时大小写无关,所以第一个单词There出现在索引表中,不是错误单词;单词verrsions没有出现在索引表中,拼写错误,所以作为出错单词输出;单词ANSI拼写成了ANS1,将其中字母都转换为小写后输出,并且多次出现,多次输出;其他出错单词类似。错误单词输出按照字典序由小到大输出到error.txt文件中。

【样例输入2】

假设文件in.txt内容为:

There are two versions of the international standard fo

文件index.txt中的单词索引表内容为:

are
for
international
of
standards
the
there
two
versions

【样例输出2】

文件error.txt中出错的单词应为:

fo
standard

【样例2说明】

文件in.txt中的单词standard没有出现在索引表文件index.txt中,所以作为错误单词输出。
注意:样例2中in.txt文件内容还不完整,最后的单词fo后没有任何字符,fo也没有出现在索引表中,所以也作为错误单词输出。

【评分标准】本题要求对文章的单词进行检查,提交程序文件名为words.c或words.cpp。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    FILE *f, *g,*h;
    char word[20], c, tmp[20];
    char words[100][20],tr[100][20],err[100][20];
    int i, j = 0, k = 0,m=0;
    f = fopen("in.txt", "r");
    h = fopen("index.txt", "r");
    g = fopen("error.txt", "w");
    while (!feof(f)) {
        fscanf(f, "%s", words[k++]);
    }
    rewind(f);
    while (!feof(h)) {
        fscanf(h, "%s", tr[m++]);
    }
    rewind(h);

    for (i = 0; i < k; i++) {
        for (j = 0; j < strlen(words[i]); j++) {
            c = words[i][j];
            if ((c <= 'z'&&c >= 'a') || (c <= 'Z'&&c >= 'A')) {
                if ((c <= 'Z'&&c >= 'A')) {
                    c += 32;
                }
                words[i][j] = c;
            }
            else if (c == '\'') {
                int a = j, t = 0;
                while (words[i][a++] != ' ') {
                    words[k][t++] = words[i][a];
                }
                k++; 

            }
            else {

                words[i][j] = '\0';
            }
        }
    }
    int flag = 0,tp=0;
    for (i = 0; i < k; i++) {
        if ((words[i][0] == '\0')) {
            continue;
        }
        for (j = 0; j < m; j++) {
            if (strcmp(words[i], tr[j]) == 0) {
                flag = 1;
            }
        }
        if (!flag) {
            strcpy(err[tp++] , words[i]);
        }
        flag = 0;

    }
    for (i = 0; i < tp; i++) {
        for (j = i + 1; j < tp; j++) {
            if (strcmp(err[i], err[j]) > 0) {
                strcpy(word, err[i]);
                strcpy(err[i], err[j]);
                strcpy(err[j], word);
            }
        }
        fprintf(g, "%s\n", err[i]);
    }
    fclose(f);
    fclose(g);
    fclose(h);
    return 0;
}

  • 8
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值