I need help, I want to count the number of times each character appears in a file.txt and then calculate the percentage of each character in the file. If any one already has the code it would be really useful!!!Code in C to count the number of times each character appears in a file and then calculate the percentage.?
The algorithm is quite simple. I will assume that you are dealing with ascii files, unicode makes things a little (but not hugely) more complicated.
We use getchar[1] to read each character and then increment an array thats indexed by ascii code. Code will make this a little more clear.
char temp;
char count[256];
while( temp = getchar() != EOF) {
count[temp]++;
}
This will read from stdin, but it won't be very much more work to make it read from a file.
Hope this helped!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment