Program 10 - CSU1128 - CSE 2026 - Shoolini University

Program to count the number of vowels and consonents from the sentence entered by the user

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128P)
 * Instructor: Abdullahi Adem | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
#include <ctype.h>
int main()
{
    char d[100];
    int m = 0, n = 0;
    printf("\n\n -- This program counts the number of vowels and consonents from the sentence entered by the user --\n\n");
    printf("Enter a string: ");
    fgets(d, sizeof(d), stdin); // read the string from the user using fgets

    char *j = d; // create a pointer to the first character in the string

    while (*j != '\0') // iterate until the null character is reached
    {
        if (isalpha(*j)) // check if the character is a letter
        {
            if (tolower(*j) == 'a' || tolower(*j) == 'e' || tolower(*j) == 'i' || tolower(*j) == 'o' || tolower(*j) == 'u')
                m++;
            else
                n++;
        }

        j++; // move the pointer to the next character
    }

    printf("\tNumber of vowels: %d\n", m);
    printf("\tNumber of consonants: %d\n", n);

    return 0;
}                    
                

Output

-- This program counts the number of vowels and consonents from the sentence entered by the user --

Enter a string: this is a string.
 Number of vowels: 4
 Number of consonants: 9