Reputation: 43
I have recently found it difficult to identify vowels on my work, which my teacher asked me too do, does anybody have a way of doing it?...currently my code is.
Dim mystring As String
Dim isitavowel As Boolean
Dim VOWELCOUNT As Integer
Sub Main()
Console.WriteLine("Enter the text of your choice here")
mystring = Console.ReadLine
VOWELCOUNT = 0
For i = 1 To mystring.Length
isitavowel = False
If mystring(i - 1) = "a" Or mystring(i - 1) = "A" Then isitavowel = True
If mystring(i - 1) = "e" Or mystring(i - 1) = "E" Then isitavowel = True
If mystring(i - 1) = "i" Or mystring(i - 1) = "I" Then isitavowel = True
If mystring(i - 1) = "o" Or mystring(i - 1) = "O" Then isitavowel = True
If mystring(i - 1) = "u" Or mystring(i - 1) = "U" Then isitavowel = True
If isitavowel = True Then
VOWELCOUNT = VOWELCOUNT + 1
End If
Next
Console.WriteLine("That had " & VOWELCOUNT & " vowel's in it")
Console.ReadLine()
The problem is that sometimes it has errors. please help!
Upvotes: 3
Views: 4530
Reputation:
I optimized the code for you. This should give you a result quick and nice:
Private vowels As String = "aeiou"
Sub Main()
Console.WriteLine("Enter the text of your choice here")
Dim mystring As String = Console.ReadLine.ToLower
Dim VOWELCOUNT As Integer = 0
For Each c As Char In mystring
If vowels.Contains(c) Then VOWELCOUNT += 1
Next
Console.WriteLine("String contained {0} vowels in it", VOWELCOUNT)
Console.ReadLine()
End Sub
You state you are fairly new to coding so to go through the code:
If value Then
or If Not value Then
Upvotes: 3
Reputation: 26386
You can achieve this in several ways
But because this homework, you will need to read up on the last two, and for now, I will only help you
For k = 1 To kMyString.Length
Dim xter = kMyString(k - 1).ToLower 'convert to lowercase
kVowel = False
If xter = "a" or xter = "e" or xter = "i" or xter = "o" or xter = "u" Then
kVowel = true
VOWELCOUNT = VOWELCOUNT + 1
End If
Next
Your code is shorter now. Or as someone suggested
For k = 1 To kMyString.Length
Dim xter = kMyString(k - 1).ToLower 'convert to lowercase
If InStr("aeiou", xter) > 0 Then //"aeiou".Contains(xter)
VOWELCOUNT += 1
End If
Next
This looks like using calculator in exam when you are supposed to show your calculations. Might not be good for assignment but will help you later
Dim VOWELCOUNT As Integer = kMyString.Count(Function(v) "aeiou".Contains(v))
Upvotes: 0
Reputation: 57
I have edited and improved upon your code here is my solution you shouldn't get any problems with this one.
Module Module1
Dim kMyString As String
Dim kVowel As Boolean
Dim kVowelNumber As Integer
Dim kAnswer As Integer = 0
Sub Main()
Console.ForegroundColor = ConsoleColor.DarkGray
Console.WriteLine("Enter your sentence below:")
Console.ForegroundColor = ConsoleColor.DarkCyan
kMyString = Console.ReadLine
Console.ForegroundColor = ConsoleColor.DarkGray
For k = 1 To kMyString.Length
Console.Write(kMyString(k - 1))
kVowel = False
If kMyString(k - 1) = "a" Or kMyString = "A" Then kVowel = True
If kMyString(k - 1) = "e" Or kMyString = "E" Then kVowel = True
If kMyString(k - 1) = "i" Or kMyString = "I" Then kVowel = True
If kMyString(k - 1) = "o" Or kMyString = "O" Then kVowel = True
If kMyString(k - 1) = "u" Or kMyString = "U" Then kVowel = True
If kVowel Then
Console.WriteLine(" is a vowel")
kAnswer = (kAnswer + 1)
Else
Console.WriteLine(" isn't a vowel")
End If
Next
Console.ForegroundColor = ConsoleColor.DarkRed
Console.WriteLine("There are " & kAnswer & " vowels!")
Console.ReadLine()
End Sub
End Module
Hope this helps!
Upvotes: 2