简体   繁体   English

查找输入文本文件中出现频率最高的字符

[英]Finding the most frequent character of an input text file

I am trying to read an input txt file from command line and find the most frequent character in that file for a school project.我正在尝试从命令行读取一个输入 txt 文件,并在该文件中为学校项目找到最常见的字符。 I can open the txt file and print it without an issue with the following code.我可以使用以下代码打开 txt 文件并打印它而不会出现问题。 Also the funcion below freqcount(), works perfectly when I give it a string from the command line.当我从命令行给它一个字符串时,freqcount() 下面的函数也能完美地工作。 But I can't seem to make them work together.但我似乎无法让他们一起工作。 I think I'm messing up something while setting up the dest array down below.我想我在下面设置 dest 数组时弄乱了一些东西。 Any help would be appreciated.任何帮助,将不胜感激。

Also, for non static sized strings, which one is generally better to use, malloc or calloc ?此外,对于非 static 大小的字符串,通常使用哪个更好, malloc还是calloc

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

#define DEST_SIZE 26 // An arbitrary size but longest string to work is 24

char freqcount(char * str){

    // Construct character count array from the input 
    // string. 
    int len = strlen(str); 
    int max = 0;  // Initialize max count 
    char result;   // Initialize result 
    int count[255] = {0};

    // Traversing through the string and maintaining 
    // the count of each character 
    for (int i = 0; i < len; i++) { 
        count[str[i]]++; 
        if (max < count[str[i]]) { 
            max = count[str[i]]; 
            result = str[i]; 
        } 
    } 

    return result; 
}

//////////////////////////////////////////////////////////////////////

int main(int argc,char ** argv){
    int i=0;
    char dest[DEST_SIZE] = {0};

    if(argc !=2){
        perror("Error: ");
        return -1;
    }

    FILE * f = fopen(argv[1], "r");
    if (f == NULL) {
        return -1;
    }
    int c;

    while ( (c=fgetc(f)) != EOF && i++<DEST_SIZE ) {
        printf("%c",c);
        dest[i]=c;
        char cnt=freqcount(dest);
        printf("%c",cnt);
    }

    return EXIT_SUCCESS;
}

Sorry I forgot to add, originally call was after the loop such as;对不起,我忘了补充,原来调用是在循环之后,例如; (omitted the first part) (省略第一部分)

while ( (c=fgetc(f)) != EOF && i++<DEST_SIZE ) {
        printf("%c",c);
        dest[i]=c;
    }
    /*int l;
    for (l=0; l<DEST_SIZE;l++){
        if (dest[i] != 0){
           printf("%c\n",dest[l]); // burda da arrayi okuyor ama array 255 long oldugu icin cogu bos
     }
    }*/
    char cnt=freqcount(dest);
    printf("%s",cnt);




    return EXIT_SUCCESS;
}

when it is like this, the code returns the following with the input "An example Of the input.当它是这样时,代码返回以下输入“输入的示例。

An example
Of the input.(null)

Move the call of freqcount to after the while loop:freqcount的调用移到 while 循环之后:

while ( (c=fgetc(f)) != EOF && i++<DEST_SIZE ) {
    printf("%c",c);
    dest[i]=c;
}
dest[i]='\0';    // terminate
char cnt=freqcount(dest);
printf("%c",cnt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM