Student Project

Letter Counter VB Program
Ability Level: Intermediate
Estimated Time: 2 hours
Objectives:

Use arrays and string manipulation methods

Materials and Resources: Visual Basic Software
Overview: Letter counting is an interesting application with real world use. Linguists count the letters used in literature as they study changes in language. Cryptographers count letters to help them break ciphers. This project will count the letters in the words and sentences entered in a text box. Characters are stored internally as numbers. Because characters are numbers there ASCII value can be used inside formulas. The Asc function converts character values into integer values.
Instructions:

1.      Create a new project.

2.      Add a text box for the user to enter the text to evaluate. Set the Multiline property to True so the user’s text will wrap at the end of the line.

3.      Add a list box or other object to use to display the counts of the letters that appear.

4.      Add a command button or menu option to start the count. Inside the counting routine:

·        Clear all counters and displays so that a new text will not include information from a previous count.

·        Clear the area or areas where results will be displayed.

·        Create a loop that will examine each individual character.

·        If the character is a letter, add to the count for that letter.

·        Evaluate the next character until all characters have been evaluated.

·        Display the number of times each character appeared. Do not list any character that did not appear in the string.

Add a command button or menu option to exit the program

 

 
Project Extras:

 

Count the number of unique letters in the text string.

Count characters other than letters.

Count words. Most words will end with a space. The last word will usually not have a space after it.

Count sentences. Remember that periods are not the only characters that end sentences.

Report on the number of words in each sentence.

Report the average number of letters in each word.

Report the letter or letters that appear the most times or the fewest times.

   
Hints:

 

This program should correctly count the occurrences of each letter in a text message. A text string with known counts should be used to verify the correctness of the program. The program should not report zero occurrences of a letter. The program should also exit cleanly.

An integer array of 26 values should be used to store the counts for each letter. The ASCII value of each letter can be used to compute an offset into this array. For example:

sTemp = Mid(myString, I, 1)

iTemp = Asc(sTemp) - Asc("A") + 1

myCount(iTemp) += 1

Once the input string has been counted, a simple loop may be used to add the results to a list box. When the results are displayed the array should be checked for non-zero values and those values, along with a character representation of the letter should be added to a list box.

Other display options include a control array of boxes labeled with letters. When the counts are displayed, any box that does not have a value can have its visible property set to false.

These results will appear in alphabetical order because the counter array is in order. If a programmer wanted they could set the list box Sorted property to True and put the count first in the string item added to the listbox. This would put the results in order by number of occurrences in most cases. Because 10 sorts before 2 in alphabetical order, leading zeros would have to be used to keep this data in order if multiple digits were required.

Words can be counted by counting spaces. The program must take into account that the last word in the box will not generally have a trailing space. Sentences can be counted by counting periods, exclamation marks and question marks.