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

Stuart McLachlan stuart at lexacorp.com.pg
Mon Sep 5 04:29:48 CDT 2022


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
> 




More information about the AccessD mailing list