Contact Learn C
Copy Program Share Program

Program 81:To count vowels in sentence

Program 81:
 
#include<stdio.h>
#include<string.h>
main()
{
    int i,vowelcount=0;
    char str[100];
    printf("Enter a string\n");
    gets(str);
    for(i=0;i<strlen(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')
        {
            vowelcount++;
        }
    }
    printf("%d\n",vowelcount);
    
}
Explanation:

  1. This program starts with initializing :
    • str[100] → To store string with length of 100 which means it can store 100 letters
    • i →Used as helping variable
    • vowelcount→ To store number of vowelsof given string and is initilized to zero.
  2. printf("Enter a string\n");
        gets(str);
    takes the string from user
  3. for(i=0;i<strlen(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')
            {
                vowelcount++;
            }
        }
    It will iterate from 0 to the length of the string (i.e. from our example of "Now You See Me" it will iterate from 0 to 13 as length of string is 14.)
  4. Each time it iterates,each character in given string is compared with either of the letters from a,e,i,o,u,A,E,I,O,U.If any character is matched with one of these letters vowelcount is incremented by 1.
  5. From our example "Now You See Me" has 6 vowels.

 Output:

To count vowels in sentence

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide