|
-
Counting Letters in VB6
is there a way i could count the amount of different letters in a sentence in visual basic? say for example i put "hello my name is biscuitbandit" into a text box, could i get the program to tell me how many a's there are, how many b's, how many c's etc etc. i stress this isnt a homework task so im not cheating at anything
biscuitbandit - re-defining the word "stupid" since 1984.
-
I've never used visual basic, but I'm sure the basic ideas are the same. Convert the string to a character array, set up an integer counter variable for each letter you want to count, then check each element of the array to see what letter it is and increment the appropriate counter (a for loop would be good for this). I'm sure there are some visual basic libraries that deal with strings better, but a character array is an easy way to do it yourself.
AMD AthlonXP 2600+ Thoroughbred B @ 200x10.5
Shuttle AN35N nForce2 Ultra 400
2x512MB Kingston PC3200 (3-3-3)
ATI Radeon 9600 Pro
40GB WD ATA-100 8MB cache
Creative 12X DVD Drive
Memorex 52X CD-RW
Running Windows XP Pro
-
.
Easiest thing to do is looping through the sting from 1 to length of the string and adding up the different characters.
---------------------------------------------------------------
- Asus M50 Laptop - C2D T9300 - 4 gig RAM - Radeon HD 3650 - Vista x64 Ultimate
- Intel i7-3770K - Asus P8Z77-V DELUXE - 32gig RAM - Radeon HD7970 Ghz - Plextor M3 256GB/120GB OCZ Vertex3
- LG BluRay - Razor Blackwidow Ultimate Keyboard - Logitech G9x
- HP ZR2740w/Asus LCD - W7 Ultimate
---------------------------------------------------------------
-
Hammerhead Shark
What ksuohio said, but with an array for me for easy lookups, or if all I need to know is one letter, just pass in the string and the letter to a function and return the count from a loop.
-
Here ya go:
Function CountChar(strMystring, strSeekChar) as Integer
Dim Y as Integer
Dim strChar as String
Dim Count as Integer
Count = 0
Y = 0
While Y < Len(strMystring)
strChar = Mid(strMystring, Y, 1)
If strChar = strSeekChar Then
Count = Count + 1
End If
Y = Y + 1
Loop
CountChar = Count
End Function
Something along those lines should do it.
-
Hammerhead Shark
Originally posted by Azuth
Here ya go:
Function CountChar(strMystring, strSeekChar) as Integer
Dim Y as Integer
Dim strChar as String
Dim Count as Integer
Count = 0
Y = 0
While Y < Len(strMystring)
strChar = Mid(strMystring, Y, 1)
If strChar = strSeekChar Then
Count = Count + 1
End If
Y = Y + 1
Loop
CountChar = Count
End Function
Something along those lines should do it.
Arg! You used a variable where you didn't need it, you used a function where you shouldn't have, and you used a while loop in exactly the same way a for loop works! 
Code:
Function CountChar(strMyString As String, strSeekChar As String) As Integer
Dim intMyStrLen As Integer
Dim intX As Integer
' Use a variable for the length! Saves the processing rainforest!
intMyStrLen = Len(strMyString)
For intX = 1 To intMyStrLen
' Do not use a variable! It is pointless to set a variable that
' is never going to be used again!
If strSeekChar = Mid(strMyString, intX, 1) Then
' The function return value in VB is created as a variable in memory,
' it is safe to increment, the End Function is like a
' "return function-name-variable;" in C/C++/Perl/Java/etc/etc/etc.
CountChar = CountChar + 1
End If
Next
End Function
Enjoy.
Edit: Formatting.
Last edited by e_dawg; 10-02-2002 at 03:32 AM.
-
thanks guys, you are most gracious
biscuitbandit - re-defining the word "stupid" since 1984.
-
-
Hammerhead Shark
Originally posted by Azuth
If you really are that picky about efficiency then VB is maybe not the best choice of languages. We could always write this routine in Assembly and then link it in as a .dll. I am sure that we could save at least 30 bytes of code and a few hundred processor cycles.
It just comes back down to the fact few really know how the process works internally... Anyone even basically versed in loops should know a for-loop is a while-loop with a counter at the bottom and logic at the top. And one should not use a variable unless you want it to a) vary, b) be used multiple times to save processing time, or c) make the code easier to read (and I admit, I will do this with outstandingly complex calculations). Since you used Len() on a variant, I guarantee more work was performed than even using a String. Mid() obviously is not the cleanest, but writing it out to a variable is futility if you only use it once, of course, running it on a string type, again, makes it faster.
-
Yes and don't forget the compiler's use of registering variables whenever it thinks it can. A lot of the time that you use a variable, it never even exists outside of the register. Therefore, there rarely any speed or efficiency advantage even when variables are overused. The only advantage is readability which is why I generally avoid having a single line of code with more than a minumum of functions. Not that I was trying to write that routine with either efficiency or readability in mind. 
Besides, talking about efficiency in a VB routine is kind of like trying to get good gas milage out of a Ford Excursion.
-
Hammerhead Shark
Originally posted by Azuth
Besides, talking about efficiency in a VB routine is kind of like trying to get good gas milage out of a Ford Excursion.
Yes, but they are good habits to have when coding -- every little bit helps, especially as systems get bigger and languages get slower.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|