I am currently trying to develop an algorithm for the following problem:

Read data from a text file such as:

the driver returns on his glorious day.
He's very happy to be safe and.sound at home, with his wife.

With that data, i need to print the length of each word in the file and the total number of words in the file. But symbols such as . or , or ' cannot be included in the word length.


I was thinking that a nested loop would work, with the outer loop running until the end of file, and the inner loop testing to see if each word contained a period, comma or apostrophe, but i am having trouble implementing this into VB. Is that the right approach or am i totally off track?

In VB you can:
1) Read the entire file into an string like
Dim ReadBuffer as String = System.IO.File.ReadAllText("C:\SourceFolder\sourceFile.txt")

2) Split the string into words using the split function like
Dim W As String() = B.Split({" "c, "."c, ","c})
Maybe you need to pass more separators between the brackets

3)Create an append text stream writer like
Dim Wri As System.IO.StreamWriter = System.IO.File.AppendText("C:\DestinationFolder\DestinationFile.txt")

4) Loop to write into the destination file like

For Each Word As String In W
    If Word.Length > 0 Then
        Wri.WriteLine(Word & " is length " & Word.Length.ToString)
    End If
Next

Hope this helps

that makes sense, thanks for the input, but when i try to use split as you have described i get an error saying "value of type '1-Dimensional array of String' cannot be converted to 'string.' My code looks like the following:

Dim words As String
        Dim split As String

        While Not (file.EndOfStream)
            words = file.ReadLine
            split = words.Split("."c, "'"c, ","c)
        End While

also for more clarification on my problem: I dont understand how to implement my algorithm into VB:

open file
read data

While not EOF
read all lines
while words include only characters( letters)
If words include punctuation (, or . or : or ; or &)
Remove punctuation
Print each word and word length
count number of words in file
Print number of words counted

close file

First on MY solution you need to define Dim split() As String

Second, the function split analizes the input string searching for the characters defined as separators and puts every piece between two separators as a new word, returning an array of words. The separator characters are stripped from the wirds returned.

On words.Split({"."c, "'"c, ","c}) you can add as many character you want to remove, so you can modify it to words.Split({"."c, "'"c, ","c, "."c, ":"c, ";"c, "&"c}).

Please note that the parenthesis and the brackets are both required.

To open the file and read the data you can:

Dim ReadBuffer as String = System.IO.File.ReadAllText("C:\SourceFolder\sourceFile.txt")

The While Words include only characters(letters, If words include punctuation Remove punctuation is enought to

Split = words.Split({"."c, "'"c, ","c, "."c, ":"c, ";"c, "&"c}).

To show each word and their length and also to print the number of words found you can, assumming you already created the stream of type text wirter as Wri

Dim NumberOfWordsFound as Integer = 0
For Each Word As String In W
If Word.Length > 0 Then
NumberOfWordsFound +=1
Wri.WriteLine(Word & " is length " & Word.Length.ToString)
End If
Next
Wri.WriteLine("Total number of words found = " & umberOfWordsFound.ToString
Wri.close

Hope this helps

that makes sense and am very glad of help but I cannot use arrays yet or open the text file as you have suggested. I am only limited to using streamreader and loops and decisions. So to simplify more, I guess my question dumbs down to how do you print every word in a text file( not including punctuation or numbers) on to its own line in a list box?

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.