|
-
Oct 9th, 2000, 06:32 AM
#1
Thread Starter
Hyperactive Member
Hi All
Does VB have a function to count each character of the alphabet in a document;
ie:
How many of each letter in the doc without codeing for each instance myself.
Cheers Gary
-
Oct 9th, 2000, 06:39 AM
#2
Well this is a small search function and it'll return the number of times something occurs in a string, I guess you'd have to call it 26 times.
Code:
Private Function occurred(ByVal searched As String, ByVal searchstr As String) As Integer
Dim pos, count As Integer
Do
pos = InStr(pos + 1, searched, searchstr)
If pos <> 0 Then count = count + 1
Loop Until pos = 0
occurred = count
End Function
Sunny
-
Oct 9th, 2000, 06:44 AM
#3
Thread Starter
Hyperactive Member
PARAMS
What params do you give it?
-
Oct 9th, 2000, 06:48 AM
#4
Code:
Private Function occurred(ByVal searched As String, ByVal searchstr As String) As Integer
You pass:
searched - the string being searched (you'd have to load the document)
searchstr - the string being sought
eg:
Code:
Dim str As String
str = "Testing123Testing"
Text1.Text = occurred(str, "123")
The result in the textbox should be one.
Sunny
-
Oct 9th, 2000, 07:26 AM
#5
Thread Starter
Hyperactive Member
CHALLENGE
If you fancy a challenge:
Here's my code with the occurred function introduced. What i would like to do is have an occurence for every letter of the alphabet and report the results the the text dialog, i'll give it a go but every little helps.
code:
----------------------------------------------------------
Option Explicit
Dim fileLocal
Dim FileNameLocal As String
Private Sub Command1_Click()
ReadFile
End Sub
Private Sub Form_Load()
DriveLetter.AddItem "c:"
DriveLetter.AddItem "d:"
DriveLetter.AddItem "e:"
End Sub
Private Sub ReadFile()
Dim szTemp As String
Dim lne$
fileLocal = FreeFile
szTemp = DriveLetter + "\" + "temp" + "\" + ReadText.Text
FileNameLocal = szTemp
Open FileNameLocal For Input As #fileLocal
Do
Line Input #fileLocal, lne$ 'read first line
Debug.Print lne$
szTemp = szTemp + lne$
Result.Text = occurred(szTemp, "o")
Loop Until EOF(fileLocal) 'use f to locate end of fil
Close #fileLocal
End Sub
'count numbers of chars occurs
Private Function occurred(ByVal searched As String, ByVal searchstr As String) As Integer
Dim pos, count As Integer
Do
pos = InStr(pos + 1, searched, searchstr)
If pos <> 0 Then count = count + 1
Loop Until pos = 0
occurred = count + " " + searchstr
End Function
-
Oct 9th, 2000, 07:31 AM
#6
Fanatic Member
I played around with this a bit and if you want just a count of how many times each letter of the alphabet occurs, try this code. Use sunnyl's function with one change to convert the string to lowercase.
Code:
Option Explicit
Private Sub Command1_Click()
Dim curChar As String
Dim counter As Integer
curChar = "a"
For counter = 1 To 26
Debug.Print curChar & " " & occurred(Text1.Text, curChar)
curChar = Chr(Asc(curChar) + 1)
Next counter
End Sub
Private Function occurred(ByVal searched As String, ByVal searchstr As String) As Integer
Dim pos, count As Integer
searched = lcase(searched)
Do
pos = InStr(pos + 1, searched, searchstr)
If pos <> 0 Then count = count + 1
Loop Until pos = 0
occurred = count
End Function
-
Oct 9th, 2000, 07:33 AM
#7
Thread Starter
Hyperactive Member
FLIIPIN ECK!
FLIPPINE ECK!... that was quick. cheers
-
Oct 9th, 2000, 08:10 AM
#8
Thread Starter
Hyperactive Member
WHOLE FILE
I have this procedure to get a line by line read, how do i get the whole contents inot one string? i'm not a wiz with VB.
-
Oct 9th, 2000, 08:13 AM
#9
Thread Starter
Hyperactive Member
NEWLINE
How do a apply newline to this?
Debug.Print curChar & " " & occurred(Result.Text, curChar)
-
Oct 9th, 2000, 08:40 AM
#10
Fanatic Member
If you are just needing to count the occurrences in a short "txt" file, this code should work. You just read the txt file into the textbox and run the count on the contents. If you need to read word documents, I am not sure how that would work.
Code:
Private Sub command2_click()
Open "C:\personel.txt" For Input As #1
Text1.Text = Input$(LOF(1), #1)
Close #1
Dim curChar As String
Dim counter As Integer
curChar = "a"
For counter = 1 To 26
Debug.Print curChar & " " & occurred(Text1.Text, curChar)
curChar = Chr(Asc(curChar) + 1)
Next counter
End Sub
[Edited by jbart on 10-09-2000 at 10:31 AM]
-
Oct 9th, 2000, 09:45 AM
#11
Thread Starter
Hyperactive Member
NEWLINE
How do a apply newline to this?
Debug.Print curChar & " " & occurred(Result.Text, curChar)
-
Oct 10th, 2000, 12:00 AM
#12
You can use vbCrlf.
Code:
str = curChar & " " & occurred(Result.Text, curChar) & vbCrlf
This would give something like:
a 1
b 2
c 3
Sunny
-
Oct 10th, 2000, 04:02 AM
#13
Thread Starter
Hyperactive Member
Already done it, i give the text box the horizontal scroll so enabling auto-wrap, many thanks anyhow.
Gary
-
Oct 10th, 2000, 08:19 AM
#14
transcendental analytic
ISNTR FOR EACH CHAR IS SLOW
therefore, here's something faster:
Code:
Dim buffer() as byte,x&,ascii&(255)
Open File for binary as #1
Get#1,,buffer
close #1
For X=0 ubound(buffer)
ascii(buffer(x))=ascii(buffer(x))+1
next X
It will open the file, read it into a byte array and then go trough it once, adding all ascii chars amount into one array.
Now to get the amount of "A"'s you can do
debug.print ascii(asc("A"))
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 10th, 2000, 09:47 AM
#15
Member
You may want to look at the scripting runtime to read the file into a string.
The following code reads a text file into a string and returns a variant array with each element containing the frequncy of that character in the string eg:
Frequency(Asc("A")) is the number of A's in the string
Option Explicit
Public Sub Main()
Dim Frequency As Variant
Dim c As Integer
Frequency = GetFrequencyDistribution(ReadFileIntoString("C:\INSTALL.LOG"))
For c = 0 To 255
Debug.Print "Frequency of Character " & CStr(c) & " = "; Frequency(c)
Next c
End Sub
Private Function GetFrequencyDistribution(s As String) As Variant
Dim c As Integer
For c = 0 To 255
GetFrequencyDistribution = GetFrequencyDistribution & Format$(CountOccurance(s, Chr$(c)))
If c < 255 Then GetFrequencyDistribution = GetFrequencyDistribution & ","
Next c
GetFrequencyDistribution = Split(GetFrequencyDistribution, ",")
End Function
Private Function ReadFileIntoString(FileName As String) As String
Dim fso As Scripting.FileSystemObject
Dim ts As Scripting.TextStream
Set fso = New Scripting.FileSystemObject
Set ts = fso.OpenTextFile(FileName, ForReading, False, TristateFalse)
ReadFileIntoString = ts.ReadAll
ts.Close
End Function
Private Function CountOccurance(ByVal s As String, c As String) As Integer
CountOccurance = (Len(s) - Len(Replace(s, c, ""))) / Len(c)
End Function
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
|
Click Here to Expand Forum to Full Width
|