Click here to Skip to main content
15,074,942 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dim str,cnt,chk,chk1
str = InputBox("Enter the character","Repeated Character")
For cnt = 1 To Len(str)
flg = 1
chk = Mid(str,cnt,1)
chk1 = Mid(str,cnt+1,1)
If chk = chk1 Then
flg = flg + 1
MsgBox chk & "-" &flg &" " &"Occurence" ,1,"Occurrence"
Else

cnt = cnt + 1
End If
NEXT

Regards
Chandan Norway
Posted
Comments
CPallini 14-Jun-13 7:34am
   
What is your problem?
i.e. what is the expected behavior of your program (and what is the observed one?)?
chandan norway 14-Jun-13 9:47am
   
if i give input "caaaeff "

the output should be like dis "a" occured 3 and "f" occured 2

how to do dis?...i'm a beginner to vbscript please help me out..
thanks in advance:)

1 solution

The basic problem you are probably having is that you aren't checking every character: a For loop automatically increments the variable, so when you don't find a match you skip a character automatically. Removing your whole Else clause will probably fix that, but that is a poor way to do things.

I would rename your variables: chk becomes lastChar and I would set it to a silly value to start with.
I would then use a For Each loop to extract each character in turn:
VB
Dim str As String = "hello paul"
Dim lastChar As Char = ControlChars.Lf
For Each thisChar As Char In str
    If thisChar = lastChar Then
        'Show the repeat
    End If
    lastChar = thisChar
Next
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900