C Program to Count the Number of Vowels in a String

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

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare a variable to store the input string and another to count the number of vowels.
  4. Input the String: Use gets or scanf to take input from the user for the string.
  5. Count the Number of Vowels: Use a loop to iterate through each character of the string and check if it is a vowel.
  6. Display the Count of Vowels: Use printf to 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 str is declared to store the input string. The variable vowelCount is used to count the number of vowels in the string. The variable i is used as a loop counter.

Step 2: Input the String

  • The program prompts the user to enter a string using printf. The gets function is used to read the string, allowing it to include spaces.

Step 3: Count the Number of Vowels

  • The program uses a for loop to iterate through each character of the string:
    • The if statement checks whether the current character is a vowel (either uppercase or lowercase).
    • If the character is a vowel, vowelCount is incremented.

Step 4: Display the Count of Vowels

  • After counting the vowels, the program displays the number of vowels in the string using the printf function.

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top