0
819views
Write a program to count number of vowels and consonants in a given sentence
1 Answer
1
9views

include <stdio.h>

void main()
{
    char sentence[80];
    int i, vowels = 0, consonants = 0, special = 0;

    printf("Enter a sentence \n");
    gets(sentence);
    for (i = 0; sentence[i] != '\0'; i++)
    {
        if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==
        'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
        (sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
        'I' || sentence[i] == 'O' || sentence[i] == 'U'))
        {
            vowels = vowels + 1;
        }
        else
        {
            consonants = consonants + 1;
        }
        if (sentence[i] =='\t' ||sentence[i] =='\0' || sentence[i] ==' ')
        {
            special = special + 1;
        }
    }
    consonants = consonants - special;
    printf("No. of vowels in %s = %d\n", sentence, vowels);
    printf("No. of consonants in %s = %d\n", sentence, consonants);
}

Program Explanation

  1. Take the sentence as input and store in the array sentence[].
  2. Initialize the variables vowels, consonants and special to zero.
  3. Using if,else statements, check if the sentence has vowels like a,e,i,o,u,A,E,I,O and U.
  4. If it has, then increment the variable vowels by 1. Otherwise increment the variable consonants by 1.
  5. If the sentence has \t, \0, & empty space, then increment the variable special by 1.
  6. Do steps 3, 4 & 5 inside a for loop.
  7. When for loop terminates, subtract the variable consonants from special.
  8. Print the variables vowels and consonants as output.
Please log in to add an answer.