an error is found on line: letters = input.Substring((count), 2)
it says that the index must be within the string
I don't see why it is an error can anyone help?


Function DigramCount(ByVal input As String)
'takes two charcters at a time out of input
'string and determines their frequencies
'and displays them in Digrams listbox

Dim count As Integer 'count is position in string
Dim length As Integer 'length of input string
Dim letter1 As Char '1st character of letters string
Dim letter2 As Char '2nd character of letters string
Dim digram(58, 58) As Integer 'array for charcters 32 to 90
Dim row As Integer 'row index for digram array
Dim column As Integer 'column index for digram array
Dim letters As String 'substring of input string

'adds spaces to end and beginning of input string
input = " " & input & " "

length = input.Length
count = 0

'steps through input string one position at a time
'and finds frequencies of digrams
For count = 0 To (length - 1)
letters = input.Substring((count), 2)
letter1 = CChar(letters.Substring(0, 1))
letter2 = CChar(letters.Substring(1, 1))
arrangeLetters(letter1, letter2)
row = ((AscW(letter1)) - 32)
column = ((AscW(letter2)) - 32)
If ((row >= 0) And (row <= 58)) Then
If ((column >= 0) And (column <= 58)) Then
digram(row, column) = 1 + digram(row, column)
End If
End If
Next count

'takes the digramArray and displays combinations
'and count for any digram that appears at least once
For row = 0 To 58
For column = 0 To 58
If digram(row, column) >= (1) Then
lstDigrams.Items.Add((ChrW(row + 32)) & _
(ChrW(column + 32)) & ": " & digram(row, column))
End If
Next column
Next row

End Function

Function arrangeLetters(ByRef letterA As Char, ByRef letterB As _
Char) As Char
'arranges letters of digram so lowest unicode is
'displayed first ie, frequency of ZA is not
'tabulated apart from AZ

Dim temp As Char 'temp is temporary variable to
'characters

'if first letter of digram's unicode is more than
'the second swap them
If ((AscW(letterA)) > (AscW(letterB))) Then
letterA = temp
letterB = letterA
letterB = temp
End If

End Function

Recommended Answers

All 3 Replies

I go idea is to use a msgbox or some other method to see the actual string. Say msgbox input, that will tell you exactly what the string is that you are trying to get the substring of. I'm guessing there isn't 2 or 3 characters in the string, so when you try to get 2, it doesn't exist. I could be wrong, but I don't have .NET installed to try it :(

Thanks I have it fixed now. My count should have been 0 to length -2 so it did not go out of the string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.