Click to See Complete Forum and Search --> : Counting Letters in VB6
biscuitbandit
09-27-2002, 06:53 AM
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 :)
Malone
09-27-2002, 06:16 PM
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.
ksuohio
10-01-2002, 08:54 PM
Easiest thing to do is looping through the sting from 1 to length of the string and adding up the different characters.
e_dawg
10-01-2002, 09:32 PM
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.
Azuth
10-02-2002, 01:56 AM
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.
e_dawg
10-02-2002, 03:23 AM
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! :(
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.
biscuitbandit
10-05-2002, 08:58 AM
thanks guys, you are most gracious :D
Azuth
10-05-2002, 05:41 PM
Originally posted by e_dawg
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! :(
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.
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. :D
e_dawg
10-05-2002, 10:03 PM
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. :D
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.
Azuth
10-06-2002, 02:39 AM
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.
e_dawg
10-06-2002, 04:38 AM
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.