C:文本文件中出现的字母

C:文本文件中出现的字母,c,C,程序通过命令行获取输入文件,并输出文本文件中每个字母的出现情况。不知道我哪里出错了 int main(int argc, char *argv[]) { char word[1000]; int a = 0; int b = 0; int d = 0; int c = 0; int e = 0; int f = 0; int g = 0; int h = 0; int i = 0; int j = 0;

程序通过命令行获取输入文件,并输出文本文件中每个字母的出现情况。不知道我哪里出错了

int main(int argc, char *argv[]) {
    char word[1000];
    int a = 0;
    int b = 0;
    int d = 0;
    int c = 0;
    int e = 0;
    int f = 0;
    int g = 0;
    int h = 0;
    int i = 0;
    int j = 0;
    int k = 0;
    int l = 0;
    int m = 0;
    int n = 0;
    int o = 0;
    int p = 0;
    int q = 0;
    int r = 0;
    int s = 0;
    int t = 0;
    int u = 0;
    int v = 0;
    int w = 0;
    int x = 0;
    int y = 0;
    int z = 0;

    int other = 0;

    int counter, lenght;

    FILE *fp = fopen(argv[1], "r");

    fgets(word, 999, fp);

    lenght = 1000;
    for(counter = 0; counter < lenght; counter++) {
        word[counter] = tolower(word[counter]);
        if (word[counter] == 'a') {
            a++;
        }
        else if (word[counter] == 'b') {
            b++;
        }
        else if (word[counter] == 'c') {
            c++;
        }
        else if (word[counter] == 'd') {
            d++;
        }
        else if (word[counter] == 'e') {
            e++;
        }
        else if (word[counter] == 'f') {
            f++;
        }
        else if (word[counter] == 'g') {
            g++;
        }
        else if (word[counter] == 'h') {
            h++;
        }
        else if (word[counter] == 'i') {
            i++;
        }
        else if (word[counter] == 'j') {
            j++;
        }
        else if (word[counter] == 'k') {
            k++;
        }
        else if (word[counter] == 'l') {
            l++;
        }
        else if (word[counter] == 'm') {
            m++;
        }
        else if (word[counter] == 'n') {
            n++;
        }
        else if (word[counter] == 'o') {
            o++;
        }
        else if (word[counter] == 'p') {
            p++;
        }
        else if (word[counter] == 'q') {
            q++;
        }
        else if (word[counter] == 'r') {
            r++;
        }
        else if (word[counter] == 's') {
            s++;
        }
        else if (word[counter] == 't') {
            t++;
        }
        else if (word[counter] == 'u') {
            u++;
        }
        else if (word[counter] == 'v') {
            v++;
        }
        else if (word[counter] == 'w') {
            w++;
        }
        else if (word[counter] == 'x') {
            x++;
        }
        else if (word[counter] == 'y') {
            y++;
        }
        else if (word[counter] == 'z') {
            z++;
        }
        else {
            other++;
        }
    }
    printf("\nCharacter frequency in %s", argv[1]);
    printf("\nCharacter   Count");
    printf("\na \t\t %d", a);
    printf("\nb \t\t %d", b);
    printf("\nc \t\t %d", c);
    printf("\nd \t\t %d", d);
    printf("\ne \t\t %d", e);
    printf("\nf \t\t %d", f);
    printf("\ng \t\t %d", g);
    printf("\nh \t\t %d", h);
    printf("\ni \t\t %d", i);
    printf("\nj \t\t %d", j);
    printf("\nk \t\t %d", k);
    printf("\nl \t\t %d", l);
    printf("\nm \t\t %d", m);
    printf("\nn \t\t %d", n);
    printf("\no \t\t %d", o);
    printf("\np \t\t %d", p);
    printf("\nq \t\t %d", q);
    printf("\nr \t\t %d", r);
    printf("\ns \t\t %d", s);
    printf("\nt \t\t %d", t);
    printf("\nu \t\t %d", u);
    printf("\nv \t\t %d", v);
    printf("\nw \t\t %d", w);
    printf("\nx \t\t %d", x);
    printf("\ny \t\t %d", y);
    printf("\nz \t\t %d", z);
    fclose(fp);
    return 0;
}
intmain(intargc,char*argv[]){
字符字[1000];
int a=0;
int b=0;
int d=0;
int c=0;
int e=0;
int f=0;
int g=0;
int h=0;
int i=0;
int j=0;
int k=0;
int l=0;
int m=0;
int n=0;
INTO=0;
int p=0;
int q=0;
int r=0;
int s=0;
int t=0;
int u=0;
int v=0;
int w=0;
int x=0;
int y=0;
int z=0;
int-other=0;
内部计数器,长度;
文件*fp=fopen(argv[1],“r”);
fgets(word、999、fp);
长度=1000;
用于(计数器=0;计数器<长度;计数器++){
字[计数器]=tolower(字[计数器]);
如果(字[计数器]=“a”){
a++;
}
else if(字[计数器]=“b”){
b++;
}
else if(字[计数器]=“c”){
C++;
}
else if(字[计数器]='d'){
d++;
}
else if(字[计数器]='e'){
e++;
}
else if(字[计数器]=“f”){
f++;
}
else if(字[计数器]=“g”){
g++;
}
else if(字[计数器]=“h”){
h++;
}
else if(字[计数器]=“i”){
i++;
}
else if(字[计数器]=“j”){
j++;
}
else if(字[计数器]=“k”){
k++;
}
else if(字[计数器]=“l”){
l++;
}
else if(字[计数器]=“m”){
m++;
}
else if(字[计数器]=“n”){
n++;
}
else if(字[计数器]=“o”){
o++;
}
else if(字[计数器]='p'){
p++;
}
else if(字[计数器]='q'){
q++;
}
else if(字[计数器]='r'){
r++;
}
else if(字[计数器]='s'){
s++;
}
else if(字[计数器]=“t”){
t++;
}
else if(字[计数器]=“u”){
u++;
}
else if(字[计数器]=“v”){
v++;
}
else if(字[计数器]=“w”){
w++;
}
else if(字[计数器]='x'){
x++;
}
else if(字[计数器]=“y”){
y++;
}
else if(字[计数器]=“z”){
z++;
}
否则{
其他++;
}
}
printf(“\n字符频率,单位为%s”,argv[1]);
printf(“\n字符计数”);
printf(“\na\t\t%d”,a);
printf(“\nb\t\t%d”,b);
printf(“\nc\t\t%d”,c);
printf(“\nd\t\t%d”,d);
printf(“\ne\t\t%d”,e);
printf(“\nf\t\t%d”,f);
printf(“\ng\t\t%d”,g);
printf(“\nh\t\t%d”,h);
printf(“\ni\t\t%d”,i);
printf(“\nj\t\t%d”,j);
printf(“\nk\t\t%d”,k);
printf(“\nl\t\t%d”,l);
printf(“\nm\t\t%d”,m);
printf(“\nn\t\t%d”,n);
printf(“\no\t\t%d”,o);
printf(“\np\t\t%d”,p);
printf(“\nq\t\t%d”,q);
printf(“\nr\t\t%d”,r);
printf(“\ns\t\t%d”,s);
printf(“\nt\t\t%d”,t);
printf(“\nu\t\t%d”,u);
printf(“\nv\t\t%d”,v);
printf(“\nw\t\t%d”,w);
printf(“\nx\t\t%d”,x);
printf(“\ny\t\t%d”,y);
printf(“\nz\t\t%d”,z);
fclose(fp);
返回0;
}

应在两列中输出一列为字母,另一列为字母出现的次数

代码中存在问题:

  • 您不包括

  • 你只读了一行,甚至不检查是否成功。您应该编写一个循环,如
    while(fgets(word,sizeof word,fp)){

  • 检查
    word
    数组中的所有字符:应在行尾停止:
    lenght=strlen(word);

  • tolower()
    不应被赋予
    char
    参数,因为在
    char
    有符号的平台上,负值会调用未定义的行为。您可以将参数强制转换为
    (无符号字符)
    以避免这种情况:
    word[counter]=tolower((无符号字符)word[counter]);

有更大的改善空间:

  • length
    拼写错误,应该是
    length

  • 您应该使用计数器数组来避免所有这些测试和所有这些显式的
    printf
    语句

  • 检查参数计数并
    fopen()
    success

  • 无需逐行读取,使用
    getc()
    读取时一次处理一个字节。但是,一次读取一个大数据块可能会更快,因为它使用更少的测试和锁

  • printf
    语句应该在末尾而不是开头输出换行符

以下是一个经过更正和简化的版本:

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

int main(int argc, char *argv[]) {
    int count[UCHAR_MAX + 1] = { 0 };
    int other, total;
    int c;
    const char *s;
    FILE *fp;

    if (argc <= 1) {
        fprintf(stderr, "missing input file\n");
        return 1;
    }
    fp = fopen(argv[1], "r");
    if (fp == NULL) {
        fprintf(stderr, "cannot open input file %s\n", argv[1]);
        return 1;
    }

    total = 0;
    while ((c = getc(fp)) != EOF) {
        count[tolower(c)] += 1;
        total++;
    }

    printf("Character frequency in %s\n", argv[1]);
    printf("Character   Count\n");
    other = total;
    for (s = "abcdefghijklmnopqrstuvwxyz"; *s; s++) {
        printf("%c:\t%9d\n", *s, count[(unsigned char)*s]);
        other -= count[(unsigned char)*s];
    }
    printf("other:\t%9d\n", other);
    fclose(fp);
    return 0;
}

这是我写的一个快速实现。它不使用FGET,但这绝对是一个选项

程序流程应该简单,但如下所示:

  • 检查参数计数是否正确
  • 声明我们需要的变量
  • 声明文件指针并尝试打开该文件
  • 如果文件没有打开,我们将出错
  • 每次读取文件中的每个字符,并将其存储到变量
    c
  • 使用我们的ascii表,我们将改变这些值,使它们在数组中处于正确的位置
  • 打印出我们所有的价值观
  • 关闭文件

    #include <stdio.h>
    
    int main(int argc, char **argv){
    
        if (argc < 2){
            printf("Not enough arguments!\n");
            return -1;
        }
    
        int A[27] = {0}, c;
    
        FILE *inFile = fopen(argv[1], "r");
        if (inFile == NULL){
            printf("The file \"%s\" could not be opened.\n", argv[1]);
            return -2;
        }
    
        while((c = fgetc(inFile)) != EOF){
            if ( c >= 'a' && c <= 'z' ){
                /* C is a lowercase character */
                c-='a'; 
                A[c]++;
            }
            else if ( c >= 'A' && c <= 'Z' ){
                /* C is an uppercase character */
                c-='A';
                A[c]++;
            }
            else if (c == '\n'){
                /* we're not counting newlines */
                continue;
            }
            else {
                A[26]++;
            }
        }
    
        /* Print out all the values except the "Other" count. */
        for (c = 0; c < sizeof A / sizeof A[0] - 1; c++){
                printf("%c: %d\n", c+'a', A[c]);
        } printf("Other: %d\n", A[26]); //Print out "Other" count
    
        /* Close our file */
        fclose(inFile);
        return 0;
    }
    
    #包括
    int main(int argc,字符**argv){
    如果(argc<2){
    printf(“参数不足!\n”);
    返回-1;
    }
    int A[27]={0},c;
    文件*in
    
    #include <stdio.h>
    
    int main(int argc, char **argv){
    
        if (argc < 2){
            printf("Not enough arguments!\n");
            return -1;
        }
    
        int A[27] = {0}, c;
    
        FILE *inFile = fopen(argv[1], "r");
        if (inFile == NULL){
            printf("The file \"%s\" could not be opened.\n", argv[1]);
            return -2;
        }
    
        while((c = fgetc(inFile)) != EOF){
            if ( c >= 'a' && c <= 'z' ){
                /* C is a lowercase character */
                c-='a'; 
                A[c]++;
            }
            else if ( c >= 'A' && c <= 'Z' ){
                /* C is an uppercase character */
                c-='A';
                A[c]++;
            }
            else if (c == '\n'){
                /* we're not counting newlines */
                continue;
            }
            else {
                A[26]++;
            }
        }
    
        /* Print out all the values except the "Other" count. */
        for (c = 0; c < sizeof A / sizeof A[0] - 1; c++){
                printf("%c: %d\n", c+'a', A[c]);
        } printf("Other: %d\n", A[26]); //Print out "Other" count
    
        /* Close our file */
        fclose(inFile);
        return 0;
    }