Counting Vowels in a String using C

Counting Vowels in a String using C

Counting Vowels in a String using C

In this blog post, we will discuss how to write a C program that takes a string input from the user and counts the number of vowels (both uppercase and lowercase) in the input string. We will also implement a function that checks whether a given character is a vowel or not, and use this function in our program.

Problem Statement

The problem is to write a C program that prompts the user to enter a string, and then counts the number of vowels present in that string. The program should consider both uppercase and lowercase vowels. Finally, the program should print out the total count of vowels found in the input string.

Approach

To solve this problem, we will follow the following approach:

  1. Prompt the user to enter a string.
  2. Declare a variable to store the count of vowels.
  3. Iterate over each character in the string.
  4. For each character, check if it is a vowel using the vowel checking function.
  5. If the character is a vowel, increment the count of vowels.
  6. After iterating over all characters, print out the total count of vowels found.

Implementation

Let’s start by implementing the vowel checking function. We will define a function named isVowel that takes a character as input and returns 1 if the character is a vowel, and 0 otherwise.

#include <stdio.h>

int isVowel(char ch) {
    ch = tolower(ch); // convert the character to lowercase for case-insensitive comparison
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
        return 1; // character is a vowel
    }
    return 0; // character is not a vowel
}

int main() {
    char str[100];
    int vowelCount = 0;
    
    printf("Enter a string: ");
    scanf("%s", str);
    
    for (int i = 0; str[i] != ''; i++) {
        if (isVowel(str[i])) {
            vowelCount++;
        }
    }
    
    printf("Total count of vowels: %dn", vowelCount);
    
    return 0;
}

Let’s understand the implementation:

  • We include the stdio.h library for input/output operations.
  • We define the isVowel function that takes a character as input and returns 1 if the character is a vowel, and 0 otherwise.
  • In the main function, we declare a character array str to store the user input string and an integer variable vowelCount to store the count of vowels.
  • We prompt the user to enter a string using the printf function and scan the input using the scanf function.
  • We iterate over each character in the string using a for loop.
  • For each character, we call the isVowel function to check if it is a vowel.
  • If the character is a vowel, we increment the vowelCount variable.
  • After iterating over all characters, we print out the total count of vowels using the printf function.
  • Finally, we return 0 to indicate successful execution of the program.
See also  Understanding Pointers in C: A Comprehensive Guide

Conclusion

In this blog post, we discussed how to write a C program that takes a string input from the user and counts the number of vowels (both uppercase and lowercase) in the input string. We also implemented a function that checks whether a given character is a vowel or not, and used this function in our program. The program successfully counts the total number of vowels found in the input string and prints out the result.

Feel free to modify the program to suit your needs or explore further enhancements, such as handling special characters or implementing additional string manipulation functionalities.

Scroll to Top
Contact Form Demo