Results 1 to 15 of 15

Thread: COUNTING CHARACTERS

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    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

  2. #2
    Guest
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    PARAMS

    What params do you give it?

  4. #4
    Guest
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    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

  6. #6
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    FLIIPIN ECK!

    FLIPPINE ECK!... that was quick. cheers

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    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.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    NEWLINE

    How do a apply newline to this?

    Debug.Print curChar & " " & occurred(Result.Text, curChar)

  10. #10
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    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]

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    NEWLINE

    How do a apply newline to this?

    Debug.Print curChar & " " & occurred(Result.Text, curChar)

  12. #12
    Guest
    You can use vbCrlf.

    Code:
    str = curChar & " " & occurred(Result.Text, curChar) & vbCrlf
    This would give something like:
    a 1
    b 2
    c 3

    Sunny


  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    Already done it, i give the text box the horizontal scroll so enabling auto-wrap, many thanks anyhow.

    Gary

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    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.

  15. #15
    Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    53
    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
  •  



Click Here to Expand Forum to Full Width