count and print the frequency of each ASCII character

This is a discussion on count and print the frequency of each ASCII character within the C Programming forums, part of the General Programming Boards category; I have a question, I dont want you guys to do the work for me. I just could really use ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    45

    count and print the frequency of each ASCII character

    I have a question, I dont want you guys to do the work for me. I just could really use some help understand the question, and maybe you could point me in the right direction.

    I want to do the work myself, but I need some help.

    How would I make a program that would count and display the frequency of each ASCII character used in a string.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    One way would be to create an array of 128 characters. Initialize all the values to 0. Then just increment the array value of the index of the current character (e.g. array[ch]++). Once you're done counting the characters, loop through the array and print out the character and number for any array element that has a value greater than 0.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Location
    p-land
    Posts
    1

    Question Help!

    how can I get a count of the characters in the arrays and the % of the occurrence?


    this is what i have so far


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    int textLenght = 0; 
    char textChar; 
    int asciiArray[128] = {0};
    
    
    int sentence[128] = {0};
    
    printf("Enter a line of text:");
    scanf("%f", &textChar);
    
    printf("FREQUENCY TABLE\n");
    printf("---------------\n");
    printf("Char Count % of Total\n");
    printf("---- ----- ----------\n");
    while ((textChar = getchar())!= '\n') {
    textLenght++; 
    asciiArray[textChar]++; 
    }
    
    
    printf("Press any key to continue . . .\n");              
    scanf("%*[^\n]");
    scanf("%*c");
    	getchar();			                         
    	system("pause");
    	exit(0);			                         
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    You're off to a good start. First, move your input gathering above your code to print the header. Then, you just have to take care of itsme86's last sentence:

    Quote Originally Posted by itsme86 View Post
    Once you're done counting the characters, loop through the array and print out the character and number for any array element that has a value greater than 0.
    Since you have 128 array elements, I might do a for loop from 0 to 127 (< 128). Percent is 100 * count / total (beware of integer division problems -- cast one operand to a float). The count is just the number stored at asciiArray[i].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Abstract classes
    By cisokay in forum C++ Programming
    Replies: 17
    Last Post: 05-29-2005, 09:39 AM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. 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