logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA
Irish Culture
Home Finance


dailyclick
All times in EST

Full Schedule
g
g ASP Site

BellaOnline's ASP Editor

g

Counting Letter Frequency in an Array

Guest Author - Lisa Shea

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
next

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
loop

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.
"
next

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.
This site needs an editor - click to learn more!

Add Counting+Letter+Frequency+in+an+Array to Twitter Add Counting+Letter+Frequency+in+an+Array to Facebook Add Counting+Letter+Frequency+in+an+Array to MySpace Add Counting+Letter+Frequency+in+an+Array to Del.icio.us Digg Counting+Letter+Frequency+in+an+Array Add Counting+Letter+Frequency+in+an+Array to Yahoo My Web Add Counting+Letter+Frequency+in+an+Array to Google Bookmarks Add Counting+Letter+Frequency+in+an+Array to Stumbleupon Add Counting+Letter+Frequency+in+an+Array to Reddit




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


For FREE email updates, subscribe to the ASP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2014 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 BellaOnline Administration for details.

g


g features
Best uses for RSS

RSS and its future.

Force Download Dialog box with ASP

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2014 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor