Saturday, May 31, 2014

Vowel Count Program in C++


Everyone of us when we started our studies our teacher in our elementary days teach us the basic alphabets from A to Z. This fundamental aspect of learning is very important to us to be familiarizing the letters in our alphabet.  Some of us did not realize the words, phrase, sentences and paragraphs we write or read is a combination of letters that are derive from our alphabets. Our teacher told us to memorize and understand the consonants and vowels in our alphabets. The most easiest one is the vowels it is only consist of a very few letter A,E,I,O and U the rest of the remaining letters in our alphabets is already considered as consonants.

The use of computers now a days is very beneficial to us we can write a program that will count the number of vowels, consonants and numbers in a given sentence of our user.  In this article I will discuss and show you how to write a computer program that I called Vowel Count Program using C++ as our programming language.  The program logic and flow is not complicated as it may seem by most readers and programmers who read this article. What the program will perform is very simple it will ask the user to enter a sentence. After the user enter the sentence the program will count the frequency of vowel characters like A, E, I, O and U I also included the ability of the program to count how many digit numbers occurs in the given sentence by the user of the program. After which it will generate a report the shows the user the frequency of occurrence of vowel characters. The good this about this program also it will summarize the number of vowels in a sentence in our report.

The first line of code is the declaration of variables that will be used later on in our program we have this following variables int a=0,e=0,i=0,o=0,u=0,sum=0, digits=0; as we can clearly see all the variable is being declared as integer as its data type. As a review an integer data type is composed of positive, zero and negative whole numbers usually all loops and iteration statements in any programming language uses integer as it main data type.

I will discuss the main code of our program that will able us to count the number of vowels and digits in our program.

while ((c=getchar())!= '\n')    /* EOF means End of File*/
 {
  if (c=='a'||c=='A')
   a=a+1;
  if (c=='e'||c=='E')
   e=e+1;
  if (c=='i'||c=='I')
   i=i+1;
  if (c=='o'||c=='O')
   o=o+1;
  if (c=='u'||c=='U')
   u=u+1;

   if (c=='1'|| c=='2' || c=='3' || c=='4' || c=='5' ||
       c=='0'|| c=='6' || c=='7' || c=='8' || c=='9')
   digits++;
 }

There is a function in C++ called getchar()  it means  get character  I am using here I while loop statement while ((c=getchar())!= '\n')     this code means very simple after the user type all the words in a given sentence and then the user will press the enter key which being indicated with the use of the escape character code in C++  \n means new line it will count the number of characters in a give sentence for it its vowels for example  if (c=='a'||c=='A')   a=a+1;  the meaning of this code is c as character encounter  small letter a or bigger letter A it will count it as 1 vowel the same procedure and code is being used with the succeeding characters.  As we observe the code is iterative we are just changing the characters that we compare in our program. For our digit  this code is being used if (c=='1'|| c=='2' || c=='3' || c=='4' || c=='5' ||   c=='0'|| c=='6' || c=='7' || c=='8' || c=='9')    digits++; in computer programming there is a characteristic of the a variable conversion called coercion principle it means a value can be converted to another data type as we can see here 1 by nature is numeric can be converted into a character by simple putting a simple single quotation in our program ‘1’.

In this particular program that I wrote using C++ I am using CodeBlocks text editor with built in Dev C++ compiler that is also an open source project that can be downloaded in the Internet in the follow web address http://www.codeblocks.org/. The good thing about the CodeBlocks text editor not only it is free to use but also is very easy to navigate and use it generates also a true executable file of your program in C++.

I hope you find my work useful in learning how to program in C++ the programming language of choice for most system programming primarily because it works well in the hardware side of the computer system.

Thank you very much.


Sample Output Of Our Program


CodeBlocks Text Editor with Dev C++ Compiler we used in creating our program


Program Listing

#include <iostream>

using namespace std;

main()
{
 int a=0,e=0,i=0,o=0,u=0,sum=0, digits=0;

 char c;

 cout <<"\n\t\t ======[ Vowel Count Program ]======";
 cout <<"\n\n";
 cout <<" Enter string :=>  ";
 while ((c=getchar())!= '\n')  /* EOF means End of File*/
 {
  if (c=='a'||c=='A')
   a=a+1;
  if (c=='e'||c=='E')
   e=e+1;
  if (c=='i'||c=='I')
   i=i+1;
  if (c=='o'||c=='O')
   o=o+1;
  if (c=='u'||c=='U')
   u=u+1;

   if (c=='1'|| c=='2' || c=='3' || c=='4' || c=='5' ||
       c=='0'|| c=='6' || c=='7' || c=='8' || c=='9')
   digits++;
 }
 sum=a+e+i+o+u;
 cout <<"\n\n";
 cout <<"\t  =====[ REPORT ]=====";
 cout <<"\n\n";
 cout <<"\n Frequency of vowel 'a' is "<< a <<".";
 cout <<"\n Frequency of vowel 'e' is " << e << ".";
 cout <<"\n Frequency of vowel 'i' is " << i <<".";
 cout <<"\n Frequency of vowel 'o' is " << o <<".";
 cout <<"\n Frequency of vowel 'u' is " << u << ".";
 cout <<"\n\n Frequency of digits is " << digits << ".";
 cout <<"\n\n";
 cout <<"\n Total no. of vowels in the text is "  <<sum << ".";
 cout <<"\n\n";
 cout << "\t\t Thank you for using this program.";
 cout <<"\n\n";
 system("PAUSE");

}

No comments:

Post a Comment