Counting Letter Frequency in an Array

Counting Letter Frequency in an Array
This sample code helps you see what arrays can be used for, and also shows a loop in action.

Let's say you want the user to input a sentence, and then you want to let them know how many times they used each letter of the alphabet. So if the user typed in:

I ate my cat.

You would return a page that said:

a: 2
c: 1
e: 1
i: 1
m: 1
t: 2
y: 1

OK, step one. You want to be counting 26 different items, one for each letter of the alphabet. This just about screams out array. So start out with a

Dim LetterCount(26)
for CurrLtr = 1 to 26
LetterCount(CurrLtr) = 0

That's a good example of a for-next loop. OK, so far so good. You have 26 spots at zero, waiting to start counting. Now to work your way through the sentence to see what is in it. Let's say you call this sentence CurrText.

First, you want to check each letter in the sentence. I'll do this with a do-while loop so you can see how that works. So you would start with:

SenLen = len(CurrText)
CurrCtr = 1
do while CurrCtr <= SenLen
CurrLtr = left(CurrText, 1)

You've got the next letter in order. Now, each letter of course has an ascii value associated with it. Those range from 65 for A to 90 for Z. Case does matter. So next you uppercase the letter to make sure it falls between 65 and 90, and subtract out 64 so that A = 1 and Z = 26.

CurrLtr = ucase(CurrLtr)
CurrLtrA = asc(CurrLtr)
CurrLtrA = CurrLtrA - 64

So now you have a number from 1 to 26 which indicates which letter this is. The next step is of course easy. You just increment that bucket! If you wish, you can make sure to ignore any non-letter, if you're worried about them getting into the stream.

if CurrLtrA > 0 and CurrLtrA < 65 then
LetterCount(CurrLtrA) = LetterCount(CurrLTrA) + 1
end if

And now the end of the loop, you get rid of the letter you were just working on so that when you go around in the loop, the first letter in the sentence is the next one in order.

CurrText = Right(CurrText, len(CurrText) - 1)
CurrCtr = CurrCtr + 1

So now your array is full of numbers ranging from 0 to whatever, each one indicating how many times that letter was found. You can display them in any way you wish. If you wanted to use a loop and the CHR function, which shows the letter that has a certain ASCII code, you could do:

for CurrLtr = 1 to 26
response.write "The letter "
CurrAscii = CurrLtr + 64
CurrLtrA = chr(CurrAscii)
response.write CurrLtrA & " was found "
response.write LetterCount(CurrLtr) & " times.

Be sure to stop by our forums or write if you have any questions!

Introduction to ASP Ebook

Download this ebook to get everything you need to know about learning ASP - from a step by step tutorial to function lists, sample code, common errors and solutions, and much more! 101 pages.

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2023 by Lisa Shea. All rights reserved.
This content was written by Lisa Shea. If you wish to use this content in any manner, you need written permission. Contact Lisa Shea for details.