The Code in C 2count the characters in a file?

This is a discussion on The Code in C 2count the characters in a file? within the C Programming forums, part of the General Programming Boards category; I need help, I want write a program in C to count the number of times each character appears in ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    The Code in C 2count the characters in a file?

    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!!!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by steven2fox View Post
    If any one already has the code it would be really useful!!!
    No it woudn't

    We prefer to help you to learn how to program, not to pass the test without learning anything.
    So if you have some questions about your assignment - show your code and ask specific question
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    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..

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21