Highlighted
Trusted Contributor.
Trusted Contributor.
1136 views

Find the occurrence of characters in a string

Jump to solution

Hi All,

I am trying to get the occurence of characters in a string.

Dim Str, ChkDup,Cnt
Str = InputBox("Enter a String to find no.of Duplicate Characters")
For i = 1 to Len(Str) 
    ChkDup=mid(Str,i,1)
    Cnt = Len(Str) - Len(Replace(Str,ChkDup,""))
    MsgBox "Character:"&ChkDup&".No of Times occured:"&Cnt

Next

 For Example, Str = "Community",above code displays output as:

C-1

O-1

m-2

m-2

u-1

n-1

i-1

t-1

y-1

Here the output for 'm' is repeating twice..Can anyone tell how to print the occurence for the characters uniquely?

0 Likes
1 Solution

Accepted Solutions
Highlighted
Honored Contributor.. Honored Contributor..
Honored Contributor..

Re: Find the occurrence of characters in a string

Jump to solution

Hello,

I haven't tested it fully but what about something like this ?

Dim Str, ChkDup,Cnt, i
Str = InputBox("Enter a String to find no.of Duplicate Characters")

Do While Len(Str) > 0
  ChkDup=Left(Str,1)
  Cnt = Len(Str) - Len(Replace(Str,ChkDup,""))
  MsgBox "Character:"&ChkDup&".No of Times occured:"&Cnt
  Str = Replace(Str,ChkDup,"")
Loop

You can always store the string into another variable if you need to keep a value of the original string.

 

View solution in original post

0 Likes
2 Replies
Highlighted
Honored Contributor.. Honored Contributor..
Honored Contributor..

Re: Find the occurrence of characters in a string

Jump to solution

Hello,

I haven't tested it fully but what about something like this ?

Dim Str, ChkDup,Cnt, i
Str = InputBox("Enter a String to find no.of Duplicate Characters")

Do While Len(Str) > 0
  ChkDup=Left(Str,1)
  Cnt = Len(Str) - Len(Replace(Str,ChkDup,""))
  MsgBox "Character:"&ChkDup&".No of Times occured:"&Cnt
  Str = Replace(Str,ChkDup,"")
Loop

You can always store the string into another variable if you need to keep a value of the original string.

 

View solution in original post

0 Likes
Highlighted
Frequent Contributor.
Frequent Contributor.

Re: Find the occurrence of characters in a string

Jump to solution

Here is another way using Dictionary object.

In this case, we take advantage of the facts that in a key-value pair, keys are always unique.

 

Dim Str, ChkDup,Cnt,d
Set d = CreateObject("Scripting.Dictionary")

Str = "Community"

For i = 1 to Len(Str)
temp = 0
'Check if the key already exists
If d.Exists(mid(Str,i,1)) Then
	temp = d.Item(mid(Str,i,1)) 'Assign item to temp
	temp = CInt(temp) + 1 'Since temp is string convert to integer
	d.Item(mid(Str,i,1)) = temp 'Update the key
else
    d.Add mid(Str,i,1), 1 'if key doesn't exist, add it
End If
Next
'Print key-value pairs
sCharacters = d.Keys ' Get the keys.
iOccurrence = d.Items ' Get the items.
For x = 0 To d.Count-1 ' Iterate the array.
msgbox  sCharacters(x) & " :" & iOccurrence(x) 
Next

 

 

------------------------------------------------------------
Please make sure to "Kudos" on the left and mark the thread as resolved if this has answered your query.
------------------------------------------------------------
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.