I need help, I want write a program in C to count the number of times each character appears in a file.txt and then calculate the percentage of each character in the file.txt. If any one already has the code it would be really useful!!!
I need help, I want write a program in C to count the number of times each character appears in a file.txt and then calculate the percentage of each character in the file.txt. If any one already has the code it would be really useful!!!
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
just get the characters one by one and on each successful get increase the count...
for percentage calculation: what you can do is make a reference table of some characters which are most probable to be there in the file..
compare the char u get by fgetc , with all in the reference table if present then increase it's count
If not there then add the char to the table..
Heres how I would do it:
-Declare an array size 256 to hold the occurrence of each character and init to 0
-Declare a variable to hold the total character count init to 0
-Open file to read (Theres an example in the FAQ)
-Read each character from the file incrementing both the total char count and the specific chars occurrence
-Close the file
-Print out each character with a char count > 0
-Print the percentage as 100.0 / total_char_count * occurrence.
That should do it.