Visual Basic Assignement

DeadlyAura

Supreme [H]ardness
Joined
Jun 6, 2005
Messages
4,857
I need some help guys (and girls of course). We have to write a VB program for my computer programming class and we can't figure somthing out. Our teacher also doesn't teach, he sits there and observes while we teach ourselves everything so therefore, I am having some trouble.

The assignment:

Write a program that will allow the user to type in as many words as they want. Your program should then display how many of each letter in the alphabet was typed in. This means, how may a’s did the person type in, how many b’s did the person type in, how many c’s did the person type in, etc. The computer should count both lower case and upper case letters as the same. Display your answer in a list box. Your program must use at least one array.

Now, I tried writing up some code but it doesn't all work, or work the way intended. What i have is this:

Code:
Option Explicit

Private Sub Command1_Click()

Dim letter
Dim text
Dim nletter As Integer
Dim i As Integer
Dim length
Dim word
Dim a
Dim lowcase

length = Len(txtIn)
nletter = 0

For i = 1 To length
letter = Mid(txtIn, i, 1)
    Select Case letter
        Case "A" To "Z"
            lowcase = LCase(letter)
    End Select
Next i

For i = 1 To length
letter = Mid(txtIn, i, 1)
Select Case lowcase
    Case "a" To "z"
        nletter = nletter + 1
    Case Else
        nletter = nletter + 0
End Select

lstOut.AddItem (letter + " = " + nletter)

Next i

End Sub

Any suggesstions?

Also, sorry for the badly written code. I've been choping away at it, throwing stuff in, taking stuff out, just trying to get SOMETHING to happen. :D
 
Is this VB6 or VB.NET?

Are you getting runtime errors?

Since he wants to know how many of each letter there is, you shouldn't be incrementing nLetter for every letter. Make nLetter into an array for each letter of the alphabet and make a case statement for each letter.
 
VB5 actually because my school is cheap. :D

But thanks, I'll try that. no runtime errors, it runs through, just doesn't work how I want it to.
 
Sorry I can't be of any help to you, but you should ask a mod to move this to the Webmastering and Programming subforum. You'll get more help there.
 
Be forewarned I'm not qualified to post. But until someone else does, here's some thoughts without leading too much:

lcase might be able to convert a whole string at a time, and it probably doesn't care about numbers/symbols. One way or another, you might be able to chop some code.

Related, you're using lowcase in your Select Case situation in the second For/Next loop, but he only place lowcase is being set is in the first For/Next loop.

As mentioned, your code currently looks like it just counts letters as a whole. Definitely consider using the array as suggested. Using ASCII values may or may not help.

You're currently calling lstOut.AddItem once for every letter in the string. The result you're probably looking for is once for every letter in the alphabet instead.
 
Is this VB6 or VB.NET?

Are you getting runtime errors?

Since he wants to know how many of each letter there is, you shouldn't be incrementing nLetter for every letter. Make nLetter into an array for each letter of the alphabet and make a case statement for each letter.
That would be the best way to approach it IMO without more advanced collection objects.

That is a really badly written assignment, IMO. It doesn't really give much information at all and it's a needlessly tedious task. Writing 26 select case/if statements as it sounds like the teacher is wanting you to is silly, and you aren't learning anything new after the first couple. And VB5? That's downright evil.

Anyway, since you're stuck doing it, how are you getting input precisely? Are they entering single characters into a text box and the clicking the button the "add" them to the count? This is a lot easier if you're getting one character at a time rather than a buffer full of them or especially strings, so I'll assume that's what you're doing here.

You're going to want to initialize an array of integers of length 26 (probably indexes 0-25, although old VB lets you change bounds if I remember correctly I'd avoid this), one spot corresponding to the count for each letter of the alphabet. Then you're going to have a select case with one case for each letter. You'll go through and ask "if this is a, add 1 to array(0) and break. if this is b, add 1 to array(1) and break" etc.

One way of doing it with a little less typing would be to create two arrays of length 26, one of integers, and one containing each character in the alphabet in ascending order. You could then write a for loop and compare the entered value to the character array at the index corresponding to your loop counter (obviously, you run through the loop 26 times or as many as it takes to get to the letter you're looking at). If they are the same character, you add 1 to whatever index you're at in the int array. This isn't really computationally more efficient (at least not significantly as you're performing the same number of comparisons and computations either way) than the last approach, it's just way way less typing and will be far easier to read.

That's normally more help than people give on homework, but it really sounds like that teacher isn't helping much, so that should get you in the right direction. Currently you're just counting how many characters there are total.
 
You should be able to knock down the number of loops/cases by remembering that a - 97 = 0, and z - 97 = 25. At least, I hope it is, it's been a while :)
 
You should be able to knock down the number of loops/cases by remembering that a - 97 = 0, and z - 97 = 25. At least, I hope it is, it's been a while :)
^^Definitely check into that...far more elegant approach than comparing concurrent array indexes.
 
Back
Top