[AccessD] Count the occurrences of each character in a string

Stuart McLachlan stuart at lexacorp.com.pg
Wed Sep 7 02:13:07 CDT 2022


Just converted this to a PB function and in doing so, I realised that it was slower than 
necessary. With a large string you will save a LOT of iterations with

For x = 1 To lStr
       If Asc(Mid$(sTxt, x, 1)) <> 0 Then
            freq(x) = 1
            For y = x + 1 To lStr
                If StrComp(Mid$(sTxt, x, 1), Mid$(sTxt, y, 1), compare) = 0 Then
                    freq(x) = freq(x) + 1
                    Mid$(sTxt, y, 1) = Chr$(0)
               End If
            Next
       End If
Next


...


On 5 Sep 2022 at 21:31, Stuart McLachlan wrote:

> For a Unicode string with potentially thousands of code points, you
> don't want an array of all possible values.
> 
> You can do it with an array the same size as the string length
> A bit more complicated than the previous code, but this one works well
>  with either ANSI or Unicode strings.
> 
> 
> Function Lettercount(sInputString As String, CaseSensitive As Boolean)
> As Long
>     Dim lStr As Long, x As Long, y As Long, compare As Long
>     Dim sTxt As String
>     Dim freq() As Long
> 
>     'Initialise
>     sTxt = sInputString 'we are going to modify the string!
>     If CaseSensitive = True Then
>         compare = vbBinaryCompare
>     Else
>         compare = vbTextCompare
>     End If
>     lStr = Len(sTxt)
>     ReDim freq(1 To lStr)
> 
>     'Get the letter counts
>     For x = 1 To lStr
>        freq(x) = 1
>        For y = x + 1 To lStr
>            If StrComp(Mid$(sTxt, x, 1), Mid$(sTxt, y, 1), compare) = 0
>            Then
>                freq(x) = freq(x) + 1
>                Mid$(sTxt, y, 1) = Chr$(0)
>           End If
>        Next
>    Next
> 
>   'Display the results
>    For x = 1 To lStr
>       If Mid$(sTxt, x, 1) <> Chr$(0) Then
>         Debug.Print Mid$(sTxt, x, 1) & "  -  " & str$(freq(x))
>       End If
>    Next
> End Function
> 
> 
> 
> 
> On 5 Sep 2022 at 19:29, Stuart McLachlan wrote:
> 
> > Classic  "bucket" or "pigeon hole" application.
> > 
> > For ANSI strings:
> > 
> > DIm arr(32 to 255) 'assumes only printable values
> > for x = 1 to len(strTest)
> >    y = aSC(mid$(strTest,x,1)
> >   arr(y) = arr(y) + 1
> > next
> > for x = 32  to 255
> >    if arr(x) > 0 then  debug.print chr$(x) & " - " & str$(arr(x))
> >    next
> > 
> > 
> > If it's a Wide String that can contain any Unicode point,  then you
> > really need to use a more complex solution such as a BTree or a
> > Collection where you check whether you already have an item with a
> > key matching the current character code and either add it or
> > increment its value.
> > 
> > 
> > 
> > On 5 Sep 2022 at 4:43, Arthur Fuller wrote:
> > 
> > > I'm trying to figure out the best way to count the occurrences of
> > > each character in a string. Sample string: "mississipi" I should
> > > get back an array looking like this:
> > > 
> > > Character Count
> > > m 1
> > > i 4
> > > s 4
> > > p 1
> > > 
> > > Any suggestions?
> > > 
> > > -- 
> > > Arthur
> > > -- 
> > > AccessD mailing list
> > > AccessD at databaseadvisors.com
> > > https://databaseadvisors.com/mailman/listinfo/accessd
> > > Website: http://www.databaseadvisors.com
> > > 
> > 
> > 
> > -- 
> > AccessD mailing list
> > AccessD at databaseadvisors.com
> > https://databaseadvisors.com/mailman/listinfo/accessd
> > Website: http://www.databaseadvisors.com
> > 
> 
> 
> -- 
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
> 




More information about the AccessD mailing list