小编Zac*_*ene的帖子

删除标点符号并使用C语言

我正在为学校编写一个程序,要求从文件中读取文本,将所有内容都大写,并删除标点符号和空格.文件"Congress.txt"包含

(国会不得制定任何法律,尊重宗教信仰,禁止自由行使;或者剥夺言论自由或新闻自由;或者人民和平集会的权利,并向政府请求补救不满.)

它读入正确,但我到目前为止删除标点,空格和大写导致垃圾字符的一些主要问题.到目前为止我的代码是:

void processFile(char line[]) {
    FILE *fp;
    int i = 0;
    char c;

    if (!(fp = fopen("congress.txt", "r"))) {
        printf("File could not be opened for input.\n");
        exit(1);
    }

    line[i] = '\0';
    fseek(fp, 0, SEEK_END);
    fseek(fp, 0, SEEK_SET);
    for (i = 0; i < MAX; ++i) {
        fscanf(fp, "%c", &line[i]);
        if (line[i] == ' ')
            i++;
        else if (ispunct((unsigned char)line[i]))
            i++;
        else if (islower((unsigned char)line[i])) {
            line[i] = toupper((unsigned char)line[i]);
            i++;
        }
        printf("%c", line[i]);
        fprintf(csis, "%c", line[i]);
    }

    fclose(fp); …
Run Code Online (Sandbox Code Playgroud)

c arrays string character-arrays output

7
推荐指数
1
解决办法
1189
查看次数

标签 统计

arrays ×1

c ×1

character-arrays ×1

output ×1

string ×1