Showing posts with label Vowels and Consonants in Visual Basic. Show all posts
Showing posts with label Vowels and Consonants in Visual Basic. Show all posts

Friday, May 30, 2014

Consonants and Vowels Checker in Visual Basic 6

Learning to read and write are very essentials to everyone that’s why education is very important for every individual in this world. Being literate is a must for us we can’t find a better job is we were not able to finish our studies. One of the basic skills that the school can give to the student is to know how to differentiate consonants and vowels in the elementary level of educational learning of the student.

This consonants and vowels are the building blocks in any words, sentences and paragraphs that we have read, write and most importantly understand. Our teacher during the preschool or kinder garden levels teaches the students that vowels are those letters that are A, E,I,O and U while the consonants are those letters that does not belong to vowels. Having this knowledge we can build our own understanding about words, sentences, phrases and paragraphs that we read from newspapers, books, magazine, e-books or news over the Internet.

Being literate in terms of our vocabulary makes us more confident person and able to express ourselves to other people that we meet. It also help us boost our self confidence specially in applying for a job and during the interview portion we can answer our interviewer with ease and confidence compared to other applicants that are not frequent or proficient with their set of vocabulary.

In this article I will discuss a program that I wrote using Microsoft Visual Basic 6 that will accept a sentence from the user and then it will count the number of consonants and vowels in a given sentence. I called this program Consonants and Vowels Program, the basic structure of our program we start with the design of our user interface that will interact to our user. We have here a label control that will ask the user to enter a sentence in our text box control let us named our text box control txt_string we are applying the programming naming convention called Hungarian Notation.  After the user type the sentence, our program has three command button the first one is called Count when the user click the count button it will call the program to count the number of consonants and vowels in our sentence. And then display the two label results in our form.

I also included another command button Clear which allows the user to clear the content of the text box and the label in our form. It makes our user in control by allowing the user to enter a new set of sentence that will evaluated again by our program.  The third and last command button is the Quit which gives the user an option whether the user will continue using the program or quit and return to our windows operating system.  I make our program shorter and more easier so that everyone with a basic knowledge and understanding about programming can easily learn by using the codes in their particular programming projects and assignments.


Sample Output of Our Program



Quit Program Screenshot



Program Listing

Const vowels = "aeiou"
Const consonants = "bcdfghjklmnpqrstvwxyz"

' This function will count the number of vowels in a given sentence
Private Function CountVowels(strText As String) As Integer
    Dim i As Integer
 Dim asciiToSearchFor As Integer
    For i = 1 To Len(LCase(strText))
        If InStr(1, vowels, Mid$(strText, i, 1), vbTextCompare) Then
            CountVowels = CountVowels + 1
         End If
    Next i
End Function

' This function will count the number of consonants in a given sentence
Private Function CountConsonants(strText As String) As Integer
    Dim i As Integer
 Dim asciiToSearchFor As Integer
    For i = 1 To Len(LCase(strText))
        If InStr(1, consonants, Mid$(strText, i, 1), vbTextCompare) Then
            CountConsonants = CountConsonants + 1
         End If
    Next i
End Function

Private Sub cmd_clear_Click()
txt_string.Text = " "
lbl_consonants.Caption = " "
lbl_vowels.Caption = " "
txt_string.SetFocus
End Sub

Private Sub cmd_count_Click()
no_vowels = CountVowels(txt_string.Text)
no_consonants = CountConsonants(txt_string.Text)
lbl_consonants.Caption = "The number of consonants in a sentence is  " & no_consonants & "."
lbl_vowels.Caption = "The number of vowels in a sentence is  " & no_vowels & "."
End Sub
'Quit Procedure
Private Sub cmd_quit_Click()
Dim Response As Integer
Response = MsgBox("Are You Sure You Want To Quit The Program", vbYesNo + vbQuestion, "Quit Program")
If Response = vbYes Then
End
Else
Me.Show
Me.txt_string.Text = ""
lbl_consonants.Caption = " "
lbl_vowels.Caption = " "
Me.txt_string.SetFocus
End If
End Sub

I hope you have learned something new in this article that I write about making a program that will count the number of consonants and vowels in a given sentence using Microsoft Visual Basic 6. 

Thank you very much