Teeheez
Teeheez

Reputation: 43

identifying vowels

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

Answers (3)

user1693593
user1693593

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:

  • In the main we define a string with all the chars we want to identify.
  • Then inside the routine we dim initializing it directly from ReadLine as this will return a string.
  • We lower-case that string so we can check fewer characters
  • Then a for-each loop will go through the string for us, char by char.
  • we check if the string containing the vowels contains that char. If it does it will return true.
  • If true THEN will allow as to count +1 for the vowels. The short form up here is the same as doing as you did. There is no difference in speed, only simpler to write. Note: in general we don't need to check = true (or = false). we can use the value directly: If value Then or If Not value Then
  • And then we present the result by formatting a string. The {0} takes the first argument we give after comma (a {1} would take the next and so forth).
  • by dimming and initializing locally in the routine you could have re-used the routine without resetting the variables (counter in this case).

Upvotes: 3

codingbiz
codingbiz

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

LINQ

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

kCoded
kCoded

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

Related Questions