
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How to count number of vowels and consonants in a string in C Language?
Problem
How to write a C program to count numbers of vowels and consonants in a given string?
Solution
The logic that we will write to implement the code to find vowels and consonants is −
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' )
If this condition is satisfied, we try to increment the vowels. Or else, we increment the consonants.
Example
Following is the C program to count the number of vowels and consonants in a string −
/* Counting Vowels and Consonants in a String */ #include <stdio.h> int main(){ char str[100]; int i, vowels, consonants; i = vowels = consonants = 0; printf("Enter any String
: "); gets(str); while (str[i] != '\0'){ 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' ){ vowels++; } else consonants++; i++; } printf("vowels in this String = %d
", vowels); printf("consonants in this String = %d", consonants); return 0; }
Output
When the above program is executed, it produces the following result −
Enter any String: TutoriasPoint vowels in this String = 6 consonants in this String = 7
- Related Articles
- C# Program to count number of Vowels and Consonants in a string
- How to Count the Number of Vowels and Consonants in a Sentence in Golang?
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- Swift Program to Count the Number of Vowels and Consonants in a Sentence
- Haskell Program to Count the Number of Vowels and Consonants in a Sentence
- Kotlin Program to Count the Number of Vowels and Consonants in a Sentence
- C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String
- C Program to count vowels, digits, spaces, consonants using the string concepts
- Alternating Vowels and Consonants in C/C++
- How to Count the Number of Vowels in a string using Python?
- C# Program to count vowels in a string
- Count consonants in a string (Iterative and recursive methods) in C++
- Replace All Consonants with Nearest Vowels In a String using C++ program
- Frequency of vowels and consonants in JavaScript
- Arrange consonants and vowels nodes in a linked list in C++?

Advertisements