Dim iArrCounter, str, strLen, temp, flValueFound, sResult
Dim arr2D(20,1)
str = “india is my country”
Dim arr2D(20,1)
str = “india is my country”
‘Initialize the first element in the array
arr2D(0,0) = “i” : arr2D(0,1) = 0 : iArrCounter = 1
strLen = Len(str)
arr2D(0,0) = “i” : arr2D(0,1) = 0 : iArrCounter = 1
strLen = Len(str)
For i = 0 to strLen – 1
flValueFound = False
flValueFound = False
‘Get the first character
temp = Left(str, 1)
temp = Left(str, 1)
‘Check if this already exists in the array. If yes, incease its count by
1. Else add it in the array and set its count as 1
1. Else add it in the array and set its count as 1
For j = 0 to iArrCounter – 1
If temp = arr2D(j, 0) Then
arr2D(j, 1) = arr2D(j, 1) + 1 ‘increment the value by 1
flValueFound = True
Exit For
End If
Next
If temp = arr2D(j, 0) Then
arr2D(j, 1) = arr2D(j, 1) + 1 ‘increment the value by 1
flValueFound = True
Exit For
End If
Next
‘If value not found in the array, add it
If flValueFound = False Then
arr2D(iArrCounter,0) = temp : arr2D(iArrCounter,1) = 1
iArrCounter = iArrCounter + 1
End If
arr2D(iArrCounter,0) = temp : arr2D(iArrCounter,1) = 1
iArrCounter = iArrCounter + 1
End If
‘Truncate the string
str = Right(str, Len(str) – 1)
str = Right(str, Len(str) – 1)
Next
‘Display the result
For k = 0 to iArrCounter – 1
sResult = sResult & arr2D(k, 0) & ” occurs ” & arr2D(k, 1) & ” times” &
Vbcrlf
Next
For k = 0 to iArrCounter – 1
sResult = sResult & arr2D(k, 0) & ” occurs ” & arr2D(k, 1) & ” times” &
Vbcrlf
Next
msgbox sResult
No comments:
Post a Comment