0

I'm making a program that takes a sentence with no punctuation as an input, then searches for how many occurrences there are of a specific word.

 Dim sntnce As String
    Dim srchWord As String
    Dim words As String()
    Dim word As String
    Dim count As Integer
    Dim index As New System.Text.StringBuilder()

    Console.WriteLine("Enter your sentence: ")
    sntnce = Console.ReadLine()
    sntnce = LCase(sntnce)

    Console.WriteLine("Enter the word you want to find the position of: ")
    srchWord = Console.ReadLine()
    srchWord = LCase(srchWord)

    words = sntnce.Split(New Char() {" "c})
    Console.WriteLine(" ")

    For Each word In words
        If word = srchWord Then
            index.Append(count)
            count += 1
        Else
            count += 1
        End If
    Next

    Console.WriteLine("Your word appears in the position(s)...")
    Console.WriteLine(index)
    Console.ReadLine()

The for loop takes the index of a word if found in a sentence and appends it to a string, but I would like to append it to an array so that the values for indexes can be outputted separately, however I can't find any solutions that help. How can I do this? Thanks

3
  • 1
    You would use a List<int> object for a variable size array. Commented Mar 19, 2017 at 15:04
  • I meant to write List(Of Integer) for vb.net Commented Mar 19, 2017 at 15:13
  • Oh ok. That makes more sense to me now. Commented Mar 19, 2017 at 15:15

1 Answer 1

1

Arrays are of fixed size only. When dealing with collections that you want to add, remove or insert use List(Of T).

I think what you want is the following:

Sub Main()
    Dim sntnce As String
    Dim srchWord As String
    Dim words As String()
    Dim word As String
    Dim count As Integer

    Console.WriteLine("Enter your sentence: ")
    sntnce = Console.ReadLine()
    sntnce = LCase(sntnce)

    Console.WriteLine("Enter the word you want to find the position of: ")
    srchWord = Console.ReadLine()
    srchWord = LCase(srchWord)

    words = sntnce.Split(New Char() {" "c})
    Console.WriteLine(" ")
    ' Create an expandable list of integers 'index_list'
    Dim index_list As New List(Of Integer)
    For index As Integer = 1 To words.Length
        ' It's recommened to use `Equals()` for string equality instead of `=`
        ' Arrays in VB.NET are 0-based so the i-th elements is located in `list(i-1)`
        If words(index - 1).Equals(srchWord) Then
            ' Add index to list if there is a match
            index_list.Add(index)
        End If
    Next
    ' Create a fixed array of strings to hold the character representation of the integer index array. 
    ' Since I know the size I can use an array instead of a list.
    Dim index_list_string As String() = New String(index_list.Count) {}
    For index As Integer = 1 To index_list.Count
        ' One to one mapping of integer to string
        index_list_string(index - 1) = index_list(index - 1).ToString()
    Next
    Console.WriteLine("Your word appears in the position(s)...")
    ' Take the array of strings and combine it into one string with commas in between using the `.Join()` function
    ' For example `{"A","B","C"} => "A, B, C"
    Console.WriteLine(String.Join(", ", index_list_string))
    Console.ReadLine()
End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

That is absolutely perfect. Thanks!
Could I have a little explanation of the things you've added. I'm can't quite wrap my head around everything that you've done.
Better yet, I added comments to the code for your benefit.
Even better! Thanks!
Once you learn LINQ that last parts greatly simplify to something like String.Join(", ", index_list.Select(Function(index) index.ToString()))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.