Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Count the Number of Vowels in a String in C

Last Updated on May 15, 2023 by Prepbytes

String manipulation is a common task in the C programming language. Strings can be used to perform a variety of operations, including counting the number of vowels in a string. Vowels are a group of letters that include the letters a, e, i, o, and u. Counting the number of vowels in a string is useful in many applications, including text processing, data analysis, and search algorithms. In this article, we will look at how to count the number of vowels in a string in C and provide sample code. We’ll also go over some tips and tricks for improving the code’s performance.

What are Vowels in C Language?

Vowels are letters that stand in for speech sounds in which the tongue, lips, or throat do not obstruct the passage of air from the mouth. The alphabet’s vowels are a, e, i, o, u.

Algorithm to Find the Count of Number of Vowels in a String in C

  1. Begin the program.
  2. Declare c, count, and s to be integer and character arrays, respectively.
  3. Display a message prompting the user to enter a string.
  4. Using the gets() function, read the string from the user and store it in the character array s.
  5. Set the variable c to a value of zero.
  6. Begin a while loop that will run until the end of the string is reached, that is, until s[c] equals the null character ‘0’.
  7. Check to see if the character s[c] is a vowel, such as ‘a,’ ‘e,’ ‘i,’ ‘o,’ or ‘u,’ or their uppercase equivalents.
  8. If s[c] is a vowel, raise the variable count by one.
  9. Increase the variable c by one.
  10. The number of vowels in the string is printed.
  11. Finish the program.

Code Implementation to Find the Count of Number of Vowels in a String in C

#include <stdio.h>
int main()
{
  int c = 0, count = 0;
  char s[1000];
  printf("Input a string\n");
  gets(s);

  while (s[c] != '\0') {
    if (s[c] == 'a' || s[c] == 'A' || s[c] == 'e' || s[c] == 'E' || s[c] == 'i' || s[c] == 'I' || s[c] =='o' || s[c]=='O' || s[c] == 'u' || s[c] == 'U')
      count++;
    c++;
  }
  printf("Number of vowels in the string: %d", count);
  return 0;
}

Output:

Input Terminal

Enter a string: 
Prepbytes

Output Terminal

Number of vowels in the string: 2

Algorithm to Find the Count of Number of Vowels in a String using Recursion in C

  1. Declare the function ‘countVowels,’ which takes two parameters: a string (char array) and its length.
  2. Check to see if the string’s length is 0 or less than 0. If it is, return 0.
  3. Check to see if the string’s first character is a vowel (a, e, i, o, u, A, E, I, O, U). If that’s the case, return 1 + countVowels(string + 1, length – 1).
  4. Return countVowels(string + 1, length – 1) if the first character isn’t a vowel.
  5. Declare a character array in the main function and accept user input.
  6. As parameters, pass the input string and its length to the ‘countVowels’ function.
  7. The number of vowels returned by the function should be printed.

Code Implementation to Find the Count of Number of Vowels in a String in C using Recursion

#include<stdio.h>
int countVowels(char string[], int length)
{
    if(length <= 0) // base case
        return 0;
    else if(string[0] == 'a' || string[0] == 'e' || string[0] == 'i' || string[0] == 'o' || string[0] == 'u' || string[0] == 'A' || string[0] == 'E' || string[0] == 'I' || string[0] == 'O' || string[0] == 'U') // check if first character is a vowel
        return 1 + countVowels(string + 1, length - 1);
    else
        return countVowels(string + 1, length - 1);
}

int main()
{
    char string[1000];
    printf("Enter a string: ");
    fgets(string, sizeof(string), stdin); // get input from user
    int length = strlen(string);
    int numVowels = countVowels(string, length); // call function to count vowels
    printf("Number of vowels in the string: %d", numVowels); // print result
    return 0;
}

Output:

Input Terminal

Enter a string: 
Prepbytes

Output Terminal

Number of vowels in the string: 2 

Conclusion
Finally, counting the number of vowels in a string is a typical task in C programming that may be beneficial in a variety of situations. The paper suggested two approaches to completing this task: a while loop and recursion. The while loop method iterates through the string, examining each character to see if it is a vowel. The recursion technique checks if the first letter of the string is a vowel and then continues recursively until the end of the string is reached. Both ways are effective, and the choice is based on the programmer’s preferences and the program’s requirements. Understanding these strategies allows programmers to optimize the execution of their code and effectively handle strings.

Frequently Asked Questions (FAQs)

Q1. In a C program, how do you find vowels?
Ans. The entered character is a vowel if either the lowercase_vowel or uppercase_vowel variable is 1 (true). If both the lowercase_vowel and uppercase_vowel variables are 0, the entered character is a consonant. Note: This program assumes that the user will enter the alphabet.

Q2. What is \0 in C?
Ans. ‘0’ is known as a NULL character or NULL terminator. As it refers to nothing, it is the character equivalent of the integer 0 (zIt is commonly used to indicate the end of a string in C.tring.

Q3. In C, how do you print the vowels in a string?
Ans. The solution is straightforward: we can store strings in a char array. Using a for loop, retrieve each character and compare it to all vowels, i.e. a, e, i, o, u, A, E, I, O, U. If the character matches, we can print the locating and character information.

Leave a Reply

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