Aon Novas Aon Novas - 3 months ago 11 Question

Saving a list of words in visual basic

How do I create a program that identifies the individual words in a sentence and store them in a list? I want to then get the program to create a list of positions for words in that list save these lists as a single file.

Module Module1

Sub Main()
Dim WordNumber As Integer = 0
Dim StartofWord As Integer = 1
Dim Text As String = ""
Dim L As Integer = 0
Dim Word As String

Console.WriteLine("Enter your sentence ")
Dim LotsofText As String = UCase(Console.ReadLine)

Console.WriteLine("Enter your word")
Word = UCase(Console.ReadLine())

If Mid(LotsofText, Len(LotsofText) - 1, 1) <> " " Then LotsofText = LotsofText + " "

For L = 1 To LotsofText.Length
If (Mid(LotsofText, L, 1)) = " " Then
WordNumber = WordNumber + 1
Text = (Mid(LotsofText, StartofWord, L - StartofWord))
StartofWord = L + 1

If Text = Word Then

End If

End If


If Not Text = Word Then
Console.WriteLine("Error word not found")

End If

Console.Write("Press Enter to Exit")

End Sub

End Module


Split your sentence by blank " " , loop over all words and add the position to a list if the current word matches your search word.

Dim Sentence = "Hello from the other side and hello from hell"
Dim SearchWord = "Hello"

If Not String.IsNullOrWhiteSpace(Sentence) Then
    SearchWord = SearchWord.Trim
    Sentence = Sentence.Trim
    Dim words = Sentence.Split(" ")
    If Not words.Contains(SearchWord) Then Return

    Dim searchWordPositions = New List(Of Integer)
    For i = 0 To words.Length - 1
        If words(i).Equals(SearchWord, StringComparison.InvariantCultureIgnoreCase) Then
        End If
    Console.WriteLine(String.Concat("Found '", SearchWord, "' at Positions: ", String.Join(", ", searchWordPositions)))
End If

Found 'Hello' at Positions: 0, 6

Instead of Console.WriteLine write it to a file.