Solved

Letter Frequency Histogram

Posted on 2006-11-05
5
935 Views
Last Modified: 2012-05-05
Hi , Does any one know of an example or tutorial of how to print a letter frequency histogram using a string phrase or word? Where only the letters used in the string would on the vertical axis and each letter's frequency would be represented by a certain character?
0
Comment
Question by:wkellya
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17878583
0
 

Author Comment

by:wkellya
ID: 17879981
Thanks, but I need something like

string= Deed

    1 2 3 4 5
d|**
e|**

1)How do I hide the letters not in the string a-z?
2)can this be done in a function that accepts string as a parameter?
0
 
LVL 24

Expert Comment

by:fridom
ID: 17880614
Well that's not too difficult if you spend some time thinking about it. You better uses some array then while displaying the result you check the content of this array. In C this might look like:

enum {MAX_ALPHABET_NUM=26};
char alphabet[MAX_ALPHABET_NUM] = {0};

for (int i = 0; i < MAX_ALPHABET_NUM; i++) {
   if (alphabet[i] > 0)
      // print the line
....

So with that both question are mostly answered.

Regards
Friedrich
0
 

Author Comment

by:wkellya
ID: 17881495
say I have a string
char string[80];

can i print it like

printf("%13s%13s%13s%13s%13s\n","10","20","30","40","50");
      for(int k=0;k<80;k++){
            printf("%7c%13c%      ",k,string[k]);

            for (int l=1; l<=string[i];l++){
                  printf("%c",'*');
            }
            printf("\n");
      }

but count each letter?
0
 
LVL 24

Accepted Solution

by:
fridom earned 250 total points
ID: 17882304
You have not assigned string any value, and you do not have any array to hole the counter for the number of characters.

So let's assume string[80] = "foobarbaz";
and you have int alphabet[26] = {0};
alphabet[0] contains the counters for the 'a's.

then you can fill the array this way (not tested just written from memory)

size_t str_len = strlen(string);

for (int i = 0; i < str_len; i++) {
   char ch = string[i];
   alphabet[tolower(ch) - 'a']++;
}

Then after going through string you have the numbers collected in alphabet

You then can print it as you like
ch = 'a';
for (int i = 0; i < 26 ; i++){
    printf("%c occurs %d times\n", ch, alphabet[i]);
    ch++;
}


Regards
Friedrich


 
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stack 22 159
mixing C++ and C code elegantly 10 153
What technology should I use for communication Raspberry Pi 2b and LapTop java application 2 113
Line meaning 9 82
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now