Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
VB
Module Module1
    Dim no As Integer
    Public Class text
        Private s As String

        Public Sub vowels()
            Dim l, i, number As Integer
            no = 0
            Console.WriteLine("type the text")
            s = Console.ReadLine()
            l = Len(s)
            Do While i < l

                If s(i) = "a" Then
                ElseIf s(i) = "A" Then
                ElseIf s(i) = "e" Then
                ElseIf s(i) = "E" Then
                ElseIf s(i) = "i" Then
                ElseIf s(i) = "I" Then
                ElseIf s(i) = "o" Then
                ElseIf s(i) = "O" Then
                ElseIf s(i) = "U" Then
                ElseIf s(i) = "u" Then
                    no = no + 1
                End If
                i = i + 1
            Loop
            number = no
            Console.WriteLine("The no of vowels is {0}", number)

        End Sub

            
    End Class
    Public Sub Main()
        Dim a As New text
        a.vowels()


        Console.ReadKey()
    End Sub

End Module


What I have tried:

i have tried it on my computer but didnt get it exactly
Posted
Updated 7-Sep-16 5:12am
v2
Comments
[no name] 7-Sep-16 9:19am    
Didn't get what exactly? Tried "it"? What is "it"? Just dumping your unformatted, hard to read code into a posting is not asking a question or describing a problem.
Dominic Burford 7-Sep-16 9:35am    
Step through the code in the debugger and see where the problem is. A brief look at the code shows that you are only incrementing your counter "no" when the letter "u" is matched. I'm guessing this is not what you were wanting. Also, consider re-writing this as a CASE statement rather than a bunch of If / ElseIf statements.

this

If s(i) = "a" Then
                ElseIf s(i) = "A" Then
                ElseIf s(i) = "e" Then
                ElseIf s(i) = "E" Then
                ElseIf s(i) = "i" Then
                ElseIf s(i) = "I" Then
                ElseIf s(i) = "o" Then
                ElseIf s(i) = "O" Then
                ElseIf s(i) = "U" Then
                ElseIf s(i) = "u" Then
                    no = no + 1
                End If


is truly horrible !

shouldnt vb.net be

if (some condition) then (do something)
elseif (some other condition) then (do something)
....

had you learned to use a debugger you'd likely see what your issue(s) are

how about a better way ?

declare a constant string (this is not in any language in particular)

VB
constant string vowels = "AaEeIiOoUu";


and then in your loop do a test like

VB
if vowels.contains(s[i]) then no = no + 1;


there's a better extension to what Ive shown you btw, along the lines of :-

VB
constant string vowels = "AEIOU";


VB
if vowels.contains(uppercase(s[i])) then no = no + 1;


I'll leave it up to you to properly code either of those in VB.NET
 
Share this answer
 
Comments
Member 12725211 7-Sep-16 9:38am    
i couldn't declare the constant .
Garth J Lancaster 7-Sep-16 9:49am    
its homework - if you expect me to do it all for you, well, thats not going to happen - simply google 'vb.net string constant' for example to find out exactely how to do it ....
Member 12725211 7-Sep-16 9:49am    
i have solve it now....
Member 12725211 7-Sep-16 9:50am    
i use a simple select case ..thanks for the effort though...
Your code will only count lower case 'u'. Having an empty "Then" clause is telling the code to do nothing if the condition is true. This is what your code is really doing:
VB
If s(i) = "a" Then
    ' Do nothing
ElseIf s(i) = "A" Then
    ' Do nothing
ElseIf s(i) = "e" Then
    ' Do nothing
ElseIf s(i) = "E" Then
    ' Do nothing
ElseIf s(i) = "i" Then
    ' Do nothing
ElseIf s(i) = "I" Then
    ' Do nothing
ElseIf s(i) = "o" Then
    ' Do nothing
ElseIf s(i) = "O" Then
    ' Do nothing
ElseIf s(i) = "U" Then
    ' Do nothing
ElseIf s(i) = "u" Then
    no = no + 1
End If


You do realize there are string comparisons that ignore case, right? There are also others that can look for a string (or character) in another string and tell you if it's in there?

You can do this assignment in a single IF statement.
 
Share this answer
 
VB
If s(i) = "a" Then ' Count a vowel here
ElseIf s(i) = "A" Then ' Count a vowel here
ElseIf s(i) = "e" Then ' Count a vowel here
ElseIf s(i) = "E" Then ' Count a vowel here
ElseIf s(i) = "i" Then ' Count a vowel here
ElseIf s(i) = "I" Then ' Count a vowel here
ElseIf s(i) = "o" Then ' Count a vowel here
ElseIf s(i) = "O" Then ' Count a vowel here
ElseIf s(i) = "U" Then ' Count a vowel here
ElseIf s(i) = "u" Then no = no + 1
End If

Only "u" is correct.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900