3

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;
}
1
  • 2
    make the arraay 256 big, then it's a simple count[ch]++ and you can count ANY bytes in the file. Commented Jun 14, 2013 at 21:05

4 Answers 4

5

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]);
} 
Sign up to request clarification or add additional context in comments.

3 Comments

That's dangerous because extended ASCII can cause you to walk off the end of your array. So better to make it 256 rather than 128.
@LaceCard: if (ch <= 0x7E) count[ch]++;
Presuming Ascii this would seem to work, but on a UTF8 file the counts would be off.
3

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.

4 Comments

This works for 8bit Ascii, but what about UTF8? He did says types of characters not bytes.
I used a method similar to this when I made a toy C program to count the frequency of different byte values on a given block device. It was mainly an exercise in using Apple Objective C blocks with C but you might find gist.github.com/iwillspeak/4055319#file-bytes-c-L48 of interest.
@Sqeaky fgets() can read UTF8? It returns a char promoted to int. You should use fgetwc() for a wide char.
UTF8 is variable width(as few a 1 byte as many as 4 bytes represent a sinlge character), there is no direct formula correlating how many char/bytes and how many characters. Despite this, your code seems like in practice it would work.
1

if you are reading ASCII characters:

frequency[ch]++;

where frequency is integer array of size 128

Comments

1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.