w3resource

C Programming: Count total number of vowel or consonant

C String: Exercise-9 with Solution

Write a program in C to count the total number of vowels or consonants in a string.

C Programming: Count total number of vowel or consonant

Sample Solution:

C Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define str_size 100 // Declare the maximum size of the string

int main() {
    char str[str_size]; // Declare a character array to store the string
    int i, len, vowel, cons; // Declare variables for iteration, string length, vowel count, and consonant count

    printf("\n\nCount total number of vowel or consonant :\n"); // Display information about the task
    printf("----------------------------------------------\n");
    printf("Input the string : ");
    fgets(str, sizeof str, stdin); // Read a string from the standard input (keyboard)

    vowel = 0; // Initialize vowel count to zero
    cons = 0; // Initialize consonant count to zero
    len = strlen(str); // Get the length of the input string

    // Loop to check each character of the string
    for (i = 0; i < len; i++) {
        // Check if the character is a vowel (both lowercase and uppercase)
        if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
            vowel++; // Increment the vowel count if the character is a vowel
        }
        // Check if the character is an alphabet (both lowercase and uppercase)
        else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
            cons++; // Increment the consonant count if the character is an alphabet but not a vowel
        }
    }

    // Display the total number of vowels and consonants in the string
    printf("\nThe total number of vowel in the string is : %d\n", vowel);
    printf("The total number of consonant in the string is : %d\n\n", cons);
	
	return 0; // Return 0 to indicate successful execution of the program
}

Sample Output:

Count total number of vowel or consonant :                                                                    
----------------------------------------------                                                                
Input the string : Welcome to w3resource.com                                                                  
                                                                                                              
The total number of vowel in the string is : 9                                                                
The total number of consonant in the string is : 12 

Flowchart:

Flowchart: Count total number of vowel or consonant.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to copy one string to another string.
Next: Write a program in C to find maximum occurring character in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/string/c-string-exercise-9.php