简体   繁体   中英

How to count the number of different type of characters in file using C.

The characters may contain any numeric, alphabets, symbols such as :;@ etc. one method is to use a switch case statement as show below. but thats going to be simple and long process. Is there any other method short method possible?

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

int main(void) {
FILE *fp;
fp = fopen("input.txt","r");
int ch,count[36]= {0};
if (fp == NULL)
{
fprintf(stderr,
        "Failed to open input.txt: %s\n",
         strerror(errno));
}
else
{
while ((ch = fgetc(fp)) != EOF)
{
    switch (ch)
    {
    case 'a':
        count[0]++;
        break;
    case 'b':
        count[1]++;
        break;
    default:
        count[2]++;
    }
}

fclose(fp);
}
    printf("count a is %d", count[0]);
    printf("count b is %d", count[1]);
    printf("count c is %d", count[2]);
    return 0;
}

In ASCII, printable characters have codes from 0x20 to 0x7E , so less than 128 characters. So for ASCII just use an array of 128 characters:

int count[128] = {0};

Update your count with:

count[ch]++;

and print printable characters with something like this:

for (i = 0x20; i <= 0x7E; i++)
{
    printf("count %c is %d", i, count[i]);
} 

Use an array of size 2^8 and increase the corresponding member.

while ((ch = fgetc(fp)) != EOF)
{
    characters[ ch ] += 1 ;
....

The index of the array characters fits the asci table .

if you are reading ASCII characters:

frequency[ch]++;

where frequency is integer array of size 128

If you use the functions from <ctype.h> ( isalpha , isdigit , ispunct , etc) in a series of if statements inside your while loop, you could categorize them fairly easily.

PS: for a list of these functions, see:

http://www.cplusplus.com/reference/cctype/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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