Introduction
Counting the number of vowels in a string is a common string processing task. This guide will show you how to write a C program that counts the number of vowels in a string provided by the user.
Problem Statement
Create a C program that:
- Takes a string as input from the user.
- Counts the number of vowels (both uppercase and lowercase) in the string.
- Displays the count of vowels.
Example:
- Input: String = "Hello, World!"
- Output: Number of vowels = 3
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare a variable to store the input string and another to count the number of vowels.
- Input the String: Use
getsorscanfto take input from the user for the string. - Count the Number of Vowels: Use a loop to iterate through each character of the string and check if it is a vowel.
- Display the Count of Vowels: Use
printfto display the number of vowels in the string.
C Program to Count the Number of Vowels in a String
#include <stdio.h>
int main() {
// Step 1: Declare a variable to hold the input string and a variable for counting vowels
char str[100];
int i, vowelCount = 0;
// Step 2: Prompt the user to enter a string
printf("Enter a string: ");
gets(str); // Using gets to read the string including spaces
// Step 3: Count the number of vowels in the string
for (i = 0; str[i] != '\0'; i++) {
// Check if the current character is a vowel
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') {
vowelCount++;
}
}
// Step 4: Display the count of vowels
printf("Number of vowels: %d\n", vowelCount);
return 0; // Step 5: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- The variable
stris declared to store the input string. The variablevowelCountis used to count the number of vowels in the string. The variableiis used as a loop counter.
Step 2: Input the String
- The program prompts the user to enter a string using
printf. Thegetsfunction is used to read the string, allowing it to include spaces.
Step 3: Count the Number of Vowels
- The program uses a
forloop to iterate through each character of the string:- The
ifstatement checks whether the current character is a vowel (either uppercase or lowercase). - If the character is a vowel,
vowelCountis incremented.
- The
Step 4: Display the Count of Vowels
- After counting the vowels, the program displays the number of vowels in the string using the
printffunction.
Step 5: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example 1:
Enter a string: Hello, World!
Number of vowels: 3
Example 2:
Enter a string: C Programming is fun!
Number of vowels: 6
Example 3:
Enter a string: This is a test string.
Number of vowels: 5
Conclusion
This C program demonstrates how to count the number of vowels in a string by iterating through each character and checking if it is a vowel. It covers basic concepts such as string manipulation, loops, and conditional statements, making it a useful example for beginners learning C programming.