To count the number of vowels in a given string

Thursday, December 4th, 2008

Here is a program to count number of vowels present in an entered sentence.

Logic:  Here variable ‘vowels’ is incremented whenever a vowel found in tracing. Logic behind this is very simple, that comparing each character to the set of vowels, predefined in an array. If this character is in the set of vowels then it implies that character is a vowel, so we increment the count by one.

Same logic can be applied to find the existence of  any letter set in the predetermined array.

#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int vowels = 0, i = 0;
clrscr();
printf(“nnt ENTER A STRING…: “);
gets(str);
while(str[i] != ‘′)
{
if(str[i]==’A’ || str[i]==’a’ || str[i]==’E’ || str[i]==’e’ || str[i]==’I’ || str[i]==’i’ ||
str[i]==’O’ || str[i]==’o’ || str[i]==’U’ || str[i]==’u')
vowels++;
i++;
}
printf(“nnt THE TOTAL NUMBER OF VOWELS IS…: %d”, vowels);
getch();
}
Download exe and source code here.

Avatar Image

Author Name :
Ranjith

Total : 4 Comments


4 Responses to To count the number of vowels in a given string

  1. Anand Gopal makwa Munger

    //This is C# Code

    //You can do simply Like This

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
    string str;
    str = txtString.Text;

    int vowel = 0;
    for (int n = 0; n < str.Length; n++)
    {

    switch (str[n])
    {
    case 'a':
    case 'A':
    vowel++;
    break;
    case 'e':
    case 'E':
    vowel++;
    break;
    case 'i':
    case 'I':
    vowel++;
    break;
    case 'o':
    case 'O':
    vowel++;
    break;
    case 'u':
    case 'U':
    vowel++;
    break;
    }
    }
    Response.Write(vowel);
    }
    }

  2. inday

    i dosnt disply the number of vowels. can any1 help me??

  3. nishant

    jada ho gaya beee….sory bee..

  4. Venkatesh

    Thanks a lot please help me to find maximum and minimum values in a list using pointer please please

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup