Friday, April 23, 2021

Simple Solution 1 & 2 for finding the number of Characters Occurrence using VB Script

Simple Solution 1 & 2 for finding the number of Characters Occurrence using VB Script :

Please find the TWO solution Vbs Scripts hereby attached as text and  its result screenshot and details below.


' Simple Solution 1
CountStringSimple Solution 1.Vbs
We are using Dictionary object and  we provide the Static or Dynamic input as "Meenakshisundharam Manikam"
our 'Output : M2e2n3a5k2s2h2i2u1d1r1m2 1
Execution result as below: 
image.png

' Simple Solution 2

CountStringSimple Solution 2.Vbs
We are using Dictionary object and  we provide the Static or Dynamic input as "Meenakshisundharam Manikam"
our 'Output : M2e2n3a5k2s2h2i2u1d1r1m2 1

Execution result as below: 
image.png


Code 1: 
'Simple Solution 1
'############################################## Main Script Starts ###################################################
'# Script Name:-         Finding the number of Occurrence              #
'# Script Description:- Script is to find the number of Occurrence / Duplicate Characters using VB Script.    #
'# Input Parameters:-     Given String      #
'# Output Parameters:-     Ouput String          #
'# Script date:-           04222021            #
'# Script Modification date:-  04222021            #
'# Author:-               Sundar Manikam          #
'#####################################################################################################################

Dim InputStr
'Input String Dynamic
'InputStr = InputBox("Enter a Input String for finding the number of Occurrence / Duplicate Characters in VB Script")

'Input String Static
InputStr = "Meenakshisundharam Manikam"
'InputStr = "MeeNNNaaaaKKKKKssssssHHHHHHHiiiiiiii"


Call FunSimpleCount(InputStr)

'Output : M2e2n3a5k2s2h2i2u1d1r1m2 1
'Output : M1e2N3a4K5s6H7i8


'############################################## Main Script Starts Ends ##############################################






'############################################## Function Starts ######################################################
'# Function Name:-         FnCountSimple                #
'# Function Description:- Function for finding the number of Occurrence / Duplicate Characters using VB Script.#
'# Input Parameters:-     Given String      #
'# Output Parameters:-     Output String            #
'# Function date:-           04222021            #
'# Function Modification date:- 04222021            #
'# Author :-                     Sundar Manikam      #
'#####################################################################################################################
Function FunSimpleCount(InputStr)

Dim InputStr1, InputStr2, ChkDup,Cnt, i

Do While Len(InputStr) > 0

 ChkDup=Left(InputStr,1)
 Cnt = Len(InputStr) - Len(Replace(InputStr,ChkDup,""))
 InputStr = Replace(InputStr,ChkDup,"")
 InputStr1 = ChkDup&Cnt&" "
 InputStr2 = InputStr2 + InputStr1

Loop

MsgBox InputStr2

End Function


'############################################## Function Ends ######################################################

'Code 2 :
'Simple Solution 2
'############################################## Main Script Starts ###################################################
'# Script Name:-         Finding the number of Occurrence              #
'# Script Description:- Script is to find the number of Occurrence / Duplicate Characters using VB Script.    #
'# Input Parameters:-     Given String      #
'# Output Parameters:-     Ouput String          #
'# Script date:-           04222021            #
'# Script  Modification  date:-  04222021            #
'# Author:-                    Sundar Manikam      #
'#####################################################################################################################

Dim InputStr
'Input String Dynamic
'InputStr = InputBox("Enter a Input String for finding the number of Occurrence / Duplicate Characters in VB Script")

'Input String Static
InputStr = "Meenakshisundharam Manikam"
'InputStr = "MeeNNNaaaaKKKKKssssssHHHHHHHiiiiiiii"

Call  FnCount(InputStr)

'Output : M2e2n3a5k2s2h2i2u1d1r1m2 1
'Output : M1e2N3a4K5s6H7i8

'############################################## Main Script Starts Ends ##############################################






'############################################## Function Starts ######################################################
'# Function Name:-         FnCount                  #
'# Function Description:- Function for finding the number of Occurrence / Duplicate Characters using VB Script.#
'# Input Parameters:-     Given String      #
'# Output Parameters:-     Dictionary object      #
'# Function date:-           04222021            #
'# Function Modification date:- 04222021            #
'# Author:-                     Sundar Manikam      #
'#####################################################################################################################

Function FnCount(InputStr)

Dim InputStr1,InputStr2, objDic, ChkDup,Cnt, i
Set objDic = CreateObject("Scripting.Dictionary")

For i = 1 to Len(InputStr)
temp = 0
'Check if the key already exists
If objDic.Exists(mid(InputStr,i,1)) Then
temp = objDic.Item(mid(InputStr,i,1)) 'Assign item to temp
temp = CInt(temp) + 1 'Since temp is string convert to integer
objDic.Item(mid(InputStr,i,1)) = temp 'Update the key
else
objDic.Add mid(InputStr,i,1), 1 'if key doesn't exist, add it
End If
Next

'Print key-value pairs
sCharacters = objDic.Keys ' Get the keys.
iOccurrence = objDic.Items ' Get the items.
For x = 0 To objDic.Count-1 ' Iterate the array.
'msgbox  sCharacters(x) & " :" & iOccurrence(x)
InputStr1 = sCharacters(x)&iOccurrence(x) &" "
InputStr2 = InputStr2 + InputStr1
Next

MsgBox InputStr2

End Function

'############################################## Function Ends ######################################################


No comments:

Post a Comment