Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,774 questions

51,679 answers

573 users

How to find the number of occurrences (frequency) of each character in a string with VB.NET

2 Answers

0 votes
Imports System
				
Public Module Module1
	Dim TOTLAASCII As Integer = 256
       
	Sub character_occurrences(s As String) 
		Dim arr(TOTLAASCII) As Integer
 
		Dim len As Integer = s.Length
		For i As Integer = 0 To len - 1
            arr(Convert.ToInt32(s(i))) += 1
			'Console.WriteLine(arr(Convert.ToInt32(s(i))))
		Next
       
		For i As Integer = 0 To TOTLAASCII - 1
			If arr(i) > 0 Then
				Console.WriteLine("{0} - {1}", Convert.ToChar(i), arr(i))
			End If
       	Next
	End Sub			
	Public Sub Main()
		Dim s As String = "javac++phpcpythonc#"
         
         character_occurrences(s)
	End Sub
End Module




' run:
'
' # - 1
' + - 2
' a - 2
' c - 3
' h - 2
' j - 1
' n - 1
' o - 1
' p - 3
' t - 1
' v - 1
' y - 1
'

 



answered Jan 20, 2021 by avibootz
edited Sep 4, 2021 by avibootz
0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
	Public Shared Sub Main()
        Dim s As String = "c# programming language"
        Dim dict As Dictionary(Of Char, Integer) = New Dictionary(Of Char, Integer)()

        For Each ch As Char In s.Replace(" ", String.Empty)
            If dict.ContainsKey(ch) Then
                dict(ch) = dict(ch) + 1
            Else
                dict.Add(ch, 1)
            End If
        Next

        For Each item In dict.Keys
            Console.WriteLine(item & " : " & dict(item))
        Next
    End Sub
End Class







' run:
'
' c : 1
' # : 1
' p : 1
' r : 2
' o : 1
' g : 4
' a : 3
' m : 2
' i : 1
' n : 2
' l : 1
' u : 1
' e : 1
'

 



answered Sep 4, 2021 by avibootz
...