-2

Hi I need to change WordCount and CountVowel procedures to functions and create a function to count number of consonants in a string.

I have done these two procedures but I cannot figure out how to do the last part. I am fairly new to programming.

My current code is given below:

Sub Main()
    Dim Sentence As String
    Console.WriteLine("Sentence Analysis" + vbNewLine + "")
    Console.WriteLine("Enter a sentence, then press 'Enter'" + vbNewLine + "")
    Sentence = Console.ReadLine()
    Console.WriteLine("")
    Call WordCount(Sentence)
    Call VowelCount(Sentence)
    Console.ReadLine()
End Sub
Sub WordCount(ByVal UserInput As String)
    Dim Space As String() = UserInput.Split(" ")
    Console.WriteLine("There are {0} words", Space.Length)
End Sub
Sub VowelCount(ByVal UserInput As String)
    Dim i As Integer
    Dim VowelNumber As Integer
    Dim Vowels As String = "aeiou"
    For i = 1 To Len(UserInput)
        If InStr(Vowels, Mid(UserInput, i, 1)) Then
            VowelNumber = VowelNumber + 1
        End If
    Next
    Console.WriteLine("There are {0} vowels", VowelNumber)
End Sub

Thanks for your time

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Jack
  • 9
  • 3

5 Answers5

3

I would use the following three functions. Note that WordCount uses RemoveEmptyEntries avoids counting empty words when there are multiple spaces between words.

The other two functions count upper case vowels as vowels, rather than just lower case. They take advantage of the fact that strings can be treated as arrays of Char, and use the Count method to count how many of those Chars meet certain criteria.

Note that the designation of "AEIOU" as vowels may not be correct in all languages, and even in English "Y" is sometimes considered a vowel. You might also need to consider the possibility of accented letters such as "É".

Function WordCount(UserInput As String) As Integer
    Return UserInput.Split({" "c}, StringSplitOptions.RemoveEmptyEntries).Length
End Function

Function VowelCount(UserInput As String) As Integer
    Return UserInput.Count(Function(c) "aeiouAEIOU".Contains(c))
End Function

Function ConsonantCount(UserInput As String) As Integer
    Return UserInput.Count(Function(c) Char.IsLetter(c) And Not "aeiouAEIOU".Contains(c))
End Function
Blackwood
  • 4,504
  • 16
  • 32
  • 41
  • I originally thought that the consonant function would be similar to what you have written, but I realised that it would return everything including puctuation and numbers. Sadly I'm embarrassed to admit that I didn't know about .IsLetter. It also seems that You and I are the only ones that have read the question properly so far. – David Wilson Jan 13 '16 at 18:03
2

You could use the Regex class. It's designed to search for substrings using patterns, and it's rather fast at it too.

Sub VowelCount(ByVal UserInput As String)
    Console.WriteLine("There are {0} vowels", System.Text.RegularExpressions.Regex.Matches(UserInput, "[aeiou]", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count.ToString())
End Sub

[aeiou] is the pattern used when performing the search. It matches any of the characters you've written inside the brackets.

Example:

http://ideone.com/LEYC30

Read more about Regex:

MSDN - .NET Framework Regular Expressions

MSDN - Regular Expression Language - Quick Reference

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
2

To turn each of your Sub routines into a Function, you need to do three things. First, you need to change the Sub and End Sub keywords to Function and End Function, respectively. So:

Sub MyMethod(input As String)
    ' ...
End Sub

Becomes:

Function MyMethod(input As String)
    ' ...
End Function

Next, since it's a function, it needs to return a value, so your Function declaration needs to specify the type of the return value. So, the above example would become:

Function MyMethod(input As String) As Integer
    ' ...
End Function

Finally, the code in the function must actually specify what the return value will be. In VB.NET, that is accomplished by using the Return keyword, like this:

Function MyMethod(input As String) As Integer
    Dim result As Integer
    ' ...
    Return result
End Function

So, to apply that to your example:

Sub WordCount(ByVal UserInput As String)
    Dim Space As String() = UserInput.Split(" ")
    Console.WriteLine("There are {0} words", Space.Length)
End Sub

Would become:

Function WordCount(userInput As String) As Integer
    Dim Space As String() = UserInput.Split(" ")
    Return Space.Length
End Sub

Note, ByVal is the default, so you don't need to specify it, and parameter variables, by standard convention in .NET are supposed to be camelCase rather than PascalCase. Then, when you call the method, you can use the return value of the function like this:

Dim count As Integer = WordCount(Sentence)
Console.WriteLine("There are {0} words", count)

As far as counting consonants goes, that will be very similar to your VowelCount method, except that you would give it the list of consonants to look for instead of vowels.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
1

VB is no longer a language I use frequently but I don't think I'm going to steer you wrong even without testing this out.

Sub Main()
    Dim Sentence As String
    Console.WriteLine("Sentence Analysis" + vbNewLine + "")
    Console.WriteLine("Enter a sentence, then press 'Enter'" + vbNewLine + "")
    Sentence = Console.ReadLine()
    Console.WriteLine("")

    'usually it's better just let the function calculate a value and do output elsewhere
    'so I've commented your original calls so you can see where they used to be
    'Call WordCount(Sentence)
    Console.WriteLine("There are {0} words", WordCount(Sentence))
    'Call VowelCount(Sentence)
    Console.WriteLine("There are {0} vowels", VowelCount(Sentence))
    Console.ReadLine()
End Sub

Function WordCount(ByVal UserInput As String) As Integer
    Dim Space As String() = UserInput.Split(" ")
    WordCount = Space.Length
    'or just shorten it to one line...
    'Return UserInput.Split(" ").Length
End Function

Function VowelCount(ByVal UserInput As String) As Integer
    Dim i As Integer
    Dim VowelNumber As Integer
    Dim Vowels As String = "aeiou"
    For i = 1 To Len(UserInput)
        If InStr(Vowels, Mid(UserInput, i, 1)) Then
            VowelNumber = VowelNumber + 1
        End If
    Next
    VowelCount = VowelNumber
End Function

The most obvious change between a sub and a function is changing the keywords that wrap up the procedure. For this conversation let's just say that's one good word to use for encompassing both concepts since they're very similar and many languages don't really draw such a big distinction.

For Visual Basic's purposes a function needs to return something and that's indicated by the As Integer that I added to the end of both of the function declarations (can't remember if that's the right VB terminology.) Also in VB you return a value to the caller by assigning to the name of the function (also see edit below.) So I replaced those lines that were WriteLines with appropriate assignments. Last I moved those WriteLine statements up into Main. The arguments needed to be changed to use the function return values rather than the variables they originally referenced.

Hopefully I'm not doing your homework for you!

EDIT: Visual Basic underwent a lot of changes to the language during the move to .Net back in the early 2000's. I had forgotten (or possibly not even realized) that the new preferred choice for returning a value is now more in line with languages like C#. So rather than assigning values to WordCount and VowelCount you can just use Return. One difference between the two is that a Return will cause the sub/function to exit at that point even if there is other code afterward. This might be useful inside an if...end if for example. I'm hoping this helps you learn something rather than just being confusing.

EDIT #2: Now that I see the accepted answer and re-read the question it seems there was a small part about counting consonants that got overlooked. At this point I assume this was indeed a classroom exercise and the intended answer was possibly even to derive the consonant count by using the other functions.

shawnt00
  • 16,443
  • 3
  • 17
  • 22
0

Here you go.

Function WordCount(ByVal UserInput As String) As Integer
    Dim Space As String() = UserInput.Split(" ")
    Return Space.Length
End Function

Function VowelCount(ByVal UserInput As String) As Integer
    Dim i As Integer
    Dim VowelNumber As Integer
    Dim Vowels As String = "aeiou"
    For i = 1 To Len(UserInput)
        If InStr(Vowels, Mid(UserInput, i, 1)) Then
            VowelNumber = VowelNumber + 1
        End If
    Next
    Return VowelNumber
End Function

Function ConsonantCount(ByVal UserInput As String) As Integer
    Dim i As Integer
    Dim ConsonantNumber As Integer
    Dim Consonants As String = "bcdfghjklmnpqrstvwxyz"
    For i = 1 To Len(UserInput)
        If InStr(Consonants, Mid(UserInput, i, 1)) Then
            ConsonantNumber = ConsonantNumber + 1
        End If
    Next
    Return ConsonantNumber
End Function
David Wilson
  • 4,369
  • 3
  • 18
  • 31