如何使我的C程序不只计算一个字符?

问题描述 投票:0回答:2

我想写一个程序,通过使用标准C函数接收一个文件和一个字符串,该程序计算文件中字符串所包含的所有字符。

例如,如果用户写。

counter.exe x.txt abcd

程序计算出字符串所包含的每个字符的数量: a, b ,c ,d 在文件x. txt中.

样品信息。

Number of 'a' characters in 'x.txt' file is: 12
Number of 'b' characters in 'x.txt' file is: 0
Number of 'c' characters in 'x.txt' file is: 3
Number of 'd' characters in 'x.txt' file is: 5

到目前为止,我已经能够让它打印并计算文件中的一个字符, 我如何让它计算我告诉它要计算的所有字符,而不仅仅是字符串中的第一个字符?

counter.c 代码。

#include<stdio.h>

int main() {

    int count = 0;
    char sf[20]; char rr; FILE* fp; char c;

    printf("Enter the file name :\n");
    gets(sf);

    printf("Enter the character to be counted :\n");
    scanf("%c", &rr);
    fp = fopen(sf, "r");

    while ((c = fgetc(fp)) != EOF)
    {
        if (c == rr)
            count++;
    }
    printf("File '%s' has %d instances of letter '%c'.", sf, count, rr);

    fclose(fp);
    return 0;
}

c counting
2个回答
0
投票
#define  SIZE 1024
char * malloc_buff(int dim){
    char *buf;
    buf=malloc(sizeof(char)*dim);
    if(buf==NULL){
        perror("Error in malloc");
    }
    return buf;
}


char * read_file(char * file){
    FILE* fd;
    char* file_pt;
    file_pt=malloc_buff(SIZE);
    errno=0;
    fd=fopen(file,"r+");
    if(errno!=0){
        fprintf(stderr,"error open file\n");
        exit(-1);
    }
    fread(file_pt,sizeof(char),SIZE,fd);
    if(fclose(fd)){
        fprintf(stderr,"errore close file\n");
        exit(-1);
    }
    return file_pt;
}
int main(int argc, char *argv[])
{
    char* content;
    content=malloc_buff(SIZE);
    content=read_file(argv[1]);
    int lenght_word=strlen(argv[2]);
    int counter[lenght_word];
    int i=0,x=0;
    for(x=0;x<lenght_word;x++){
        counter[x]=0;
    }
    while (content[i]!='\0'){
        for(x=0;x<lenght_word;x++){
            if (content[i]==argv[2][x]){
                counter[x]++;
            }
        }
        i++;
    }
    for(x=0;x<lenght_word;x++){
        printf("The values are: for %c is %d",argv[2][x],counter[x]);
    }
    return 0;
}

0
投票

几种解决方案 。

  1. 给你的一些输入 main() 函数,并对这些进行解析,见 此处
  2. 再做一个while循环,将 scanf() 和另一个 while 循环(以及其他你需要的东西)。
  3. 如果你想要,例如,4个字母,那么实现一个for循环,并将 scanf() 中,将值存储在一个数组中。然后你要在下一个 while 循环中添加 4 个不同的计数器,在 4 个不同的测试中更新。

顺便说一下,如果你需要这个程序做其他用途,而不仅仅是一个练习,在你的系统里有很多已经实现的命令可以做到这一点,如 grep.


0
投票

您可以使用 strpbrk 和一个查询表。

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

int main(void) 
{
    char *text = "Sample input bla bla bla"; // your file contents
    const char *find = "abcd"; // your argv[2]
    int lookup[127] = {0}; // the lookup table

    while (*text)
    {
        char str[2] = {*text, 0};
        char *ptr = strpbrk(str, find);

        if (ptr != NULL) // if found
        {
            if ((*ptr >= 0) && (*ptr < 128))
            {
                lookup[(int)*ptr]++; // increase position
            }
        }
        text++;
    }
    for (int i = 0; i < 128; i++)
    {
        if (lookup[i] != 0)
        {
            printf("%c: %d times\n", i, lookup[i]);
        }
    }
    return 0;
}

输出。

a: 4 times
b: 3 times

0
投票

我假设你可以把文件读到一个大的缓冲区里 或者一次一行读到一个小的缓冲区里

void  CountChars( unsigned int *charCount, char *buffer, int bufferSize )
{
    unsigned int  i;

    for (i = 0; i < bufferSize; i++) {
        charCount[buffer[i]]++;
    }
}

void main( int argc, char **argv )
{
    unsigned int  charCount[256];
    int           i;
    char          buffer[8192];  /* Or whatever size you want to work with at a time */

    /* Initialize the character counts to zero */
    memset(charCount, 0, sizeof(charCount));

    /* Insert code to fill the buffer with the file data */

    /* Count the characters in the buffer */
    CountChars(charCount, buffer, sizeof(buffer));

    /* At that point, the array 'charCount' has the count of each byte that
       occurred in the buffer.  If the file is too large to fit in memory
       and you called it in the loop, then you only initial the charCount
       array to zero before the first call.  From this point, you can go
       through the loop to determine which of the character counts you might
       be interested in, or if you are interested in all the characters that
       occurred, you could do the following. */
    for (i = 0; i < sizeof(charCount) / sizeof(charCount[0]); i++) {
        if (charCount[i] > 0) {
            printf("'%c' -- %d\n", (char) i, charCount[i]);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.