Help with 8.2
Exercise 8.2 Write a function called LetterHist() that takes a String as a
parameter and that returns a histogram of the letters in the String. The zeroeth
element of the histogram should contain the number of a’s in the String (upper and
lower case); the 25th element should contain the number of z’s. Your solution should
only traverse the String once.
Step this through with the following steps:
First create a histogram array that only handles a string containing a's, b's and c's
· The parameter can be a character array.
· You need the length of the string, for which you will need strlen function. That requires importing <string.h>
· Create one new array with 26 elements.
· Loop through the string as a character array, one character at a time.
o If the character is an a, add 1 to the 0 index of the 26 element array.
o If the character is a b, add 1 to the 1 index of the 26 element array.
o If the character is a c, add 1 to the 2 index of the 26 element array.
· Make another loop to print the array.
Second, find a way to calculate the index easily on all 26 lower case characters
Take advantage of the fact that an ascii 'a' is actually a number. You can subtract the integer value of 'a' from the integer value of the character you found in the string to find the index number you want. I recommend experimenting with this by just printing the result of your calculation.
Third, use your calculation of the index to set the 26 element array index for each element in the string.
Fourth, determine whether the letter is uppercase, and use a different calculation
· An ascii 'A' is a lower number than an ascii 'a'.
· Use the ctype function isupper() to determine whether it is uppercase.
· http://www.tutorialspoint.com/c_standard_library/c_function_isupper.htm
· Subtract an uppercase A rather than a lowercase 'a' for upper case letters.