-5

I am trying to write a program that finds and counts the number of characters in a a repetitive sequence in visual basic.

For example, the string is :- ABCCCCCDEFFF

Expected output:-

C = 5

F = 3

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Jonny D
  • 39
  • 6
  • 1
    You're right, you're missing any code, so there's nothing to help you with. Please read [StackOverflow's help on asking questions](http://stackoverflow.com/help/how-to-ask) and [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) first. – David Ferenczy Rogožan Oct 02 '15 at 00:37
  • I have code i was just showing the logic i was thinking, i have declared the values i just would like some help with how to do this I have tried a few options i just dont want to use a series of ifs – Jonny D Oct 02 '15 at 00:38
  • 1
    It's nice you have a code, but as I wrote if you don't show it, there is nothing to help you with. Read that help first, please. You're asking for my time, so you should also dedicate your time to ask a proper question. I'm just trying to help you yo get an answer. – David Ferenczy Rogožan Oct 02 '15 at 00:41
  • Basically i have a file that the data is read in from and i need to check for repetition of values in the string if the character is repeated more than twice a boolean is set and the program then passed these strings to a seperate list box but i know how to do that thank you very much for your time – Jonny D Oct 02 '15 at 00:50
  • Then I don't understand what you're asking for. If your question is not valid anymore, delete it, please. – David Ferenczy Rogožan Oct 02 '15 at 00:53
  • Could you please help me to determine how many characters there are in the repetitive substring? – Jonny D Oct 02 '15 at 01:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91149/discussion-between-jonny-d-and-dawid-ferenczy). – Jonny D Oct 02 '15 at 01:06
  • Can you show the code you've tried already? – Andrew Mortimer Oct 02 '15 at 06:50
  • Possible duplicate of [Count specific character occurrences in string](http://stackoverflow.com/questions/5193893/count-specific-character-occurrences-in-string) – Andrew Mortimer Oct 02 '15 at 06:52
  • Its similar but occurances must be in order – Jonny D Oct 02 '15 at 07:35

2 Answers2

1

I think it will be fine for you in order to learn a little about programming. Your question has an easy solution, and you have to make an effort to understand what's happening.

Edited:

Dim i, j, count As Integer
Dim str As String = "AAABCCCCCCDECCCFFF"
Dim myChar As Char
Dim listOfChars As New List(Of Char)
Dim listOfCount As New List(Of Integer)
Do While i < str.Length
    count = 0
    myChar = str.Chars(i)
    For j = i To str.Length - 1
        If Not str.Chars(j) = myChar Then
            listOfChars.Add(myChar)
            If count < 3 Then count = 0
            listOfCount.Add(count)
            Exit For
        ElseIf j = str.Length - 1 Then
            If str.Chars(j) = myChar Then count += 1
            listOfChars.Add(myChar)
            If count < 3 Then count = 0
            listOfCount.Add(count)
        End If
        count += 1
    Next
    If j = str.Length Then Exit Do
    i = j
Loop
For i = 0 To listOfChars.Count - 1
    Console.WriteLine("{0} = {1}", listOfChars(i), listOfCount(i))
Next

Output:

A = 3
B = 0
C = 6
D = 0
E = 0
C = 3
F = 3

The last sentence ElseIf added is not elegant at all but it works fine and handles the Array index out of bound exception.

daro
  • 313
  • 1
  • 4
  • 17
  • I appreciate your efforts and time in helping me but this code lists all occurrences of the character no matter what order they are in. The occurrences must appear directly after eachother – Jonny D Oct 02 '15 at 09:06
  • 1
    @Jonny D Give a better example what output do you want to get from code. – daro Oct 02 '15 at 09:24
  • Okay sorry, For example if there was dim str as string = "AABCCCDCF" the result would be A=0 B=0 C=3 D=0 C=0 F=0 my logic here is that the character C occurs more than twice and they are in sequence. The 8th character C does not count as it is not in the same run – Jonny D Oct 02 '15 at 09:29
  • 1
    what if you have dim str as string = "AABCCCCDCCFC"? What do you expect? – daro Oct 02 '15 at 09:37
  • daro - thanks for your response! the code you have is perfect just it doesnt read the last character in the str string? do you know why? – Jonny D Oct 05 '15 at 22:08
  • I edited my answer, I think now it works fine for you in every way. Good luck with it and please mark as accepted if it worked. Thanks! – daro Oct 06 '15 at 06:42
  • Thank you daro i sure will i didnt want to know all the answer as im trying to learn vb.net but i how would i iterate through this passing values from an array? – Jonny D Oct 06 '15 at 09:02
0

i think this should work ... You could format the output anyway you want

Function getInfo(str as String)
     If str = "" Then
         return Nothing
     End If
     dim ret as String = ""
     dim current as string = str.First
     dim count as integer = 0
     For Each s in str
         If current = s Then
            count &= 1
         Else
            ret &= current & " = " & count & " "
            count = 0
         End If
         current = s
     Next
End Function
Ikechi Michael
  • 489
  • 3
  • 6