Monday, 16 October 2017

VBscript: Finding Occurance of Each Character in the string

Dim a()
Dim strtext
'Provide the text 
strtext="this is sparta"

'Create An Array with each letter of the text
ReDim a((len(strtext)-1))
For i=1 to len(strtext) Step 1
a(i-1)=mid(strtext,i,1)
Next

'Create Dictionary Object to store unique values of the text

Set odic=CreateObject("Scripting.Dictionary")
'ReDim a((len(strtext)-1))
For i=1 to len(strtext) Step 1

If NOT odic.Exists(mid(strtext,i,1)) Then
    odic.add (mid(strtext,i,1)),"i"
End If
   
Next

alluniquekeys=odic.Keys

'Compare unique items with each item present in the array and increase the counter when there is a match
For i = 0 To ubound(alluniquekeys) Step 1
    count=0
    For j = 0 To ubound(a) Step 1
       
        If alluniquekeys(i)=a(j) Then
        count=count+1
        End If
    Next
    print " "&a(i)& "appears "&count&"times"
Next




REM===> OUTPUT
 t appears 2 times
 h appears 1 times
 i appears 2 times
 s appears 3 times
   appears 2 times
 p appears 1 times
 a appears 2 times
 r appears 1 times

No comments:

Post a Comment

Spring Boot : Exception Handler 14