# C program to Count the Number of Vowels in a String

Last Updated: Mar 23, 2023
EASY

## Introduction

A string is a data type similar to an integer and a floating-point unit that is used to represent text rather than numbers in programming. It is made up of a series of characters that may include spaces and integers. The words "hamburger" and "I ate 3 hamburgers," for example, are both strings.

There are 26 letters in English in the alphabet, five of which are vowels (a, e, i, o, u), while the rest are consonants.

The vowels a, e, i, o, and u can be lowercase or uppercase, and the computer will find and count both.

Also Read, Binary to Hex Converter.

## Problem Statement

Count the number of vowels in a string using C.

Let us see an example:

By default, the compiler appends a null character \0 to the end of a sequence of characters wrapped in double quotation marks.

Output:

### Idea

The idea behind this problem is simple, we take a count variable and traverse the string. If we find any vowel(a, e, i, o, u) then we increase the count variable by one. And in the end, we print that count variable. Hence we can find the total number of variables in a string.

### Algorithm

1. set the count to 0
2. Loop through the string until it reaches a null character.
3. Compare each character to the vowels a, e, I o, and u.
4. If both are equal, increase the count by one.
5. Print the count at the end.

### Implementation

``````/*  Count the number of vowels in a string using C */
#include <stdio.h>
#include <math.h>
#include <string.h>
void Solve()
{
unsigned short count = 0, vowels = 0;
char str[100], c;
printf("Enter a string in which you want to find number of vowels: ");
scanf("%[^\n]", str);
while(str[count] != '\0')
{
c = str[count];
if(c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I'
|| c == 'o' || c == 'O' || c == 'u' || c == 'U') {
vowels++;
printf("%c", c);
}
count++;
}
printf("\n");
printf("NUMBER OF VOWELS In Given string Are: %hu \n", vowels);
}
int main()
{
Solve();
return 0;
}``````

Time Complexity: O(N) where n is the length of the string.

Auxiliary Space: Constant space.

Output

### Program Explanation

Here is the process. The if condition checks for both lowercase and uppercase vowels inside the while loop. The vowels variable is incremented if the requirement is met.

The printf() statement also prints the vowel included within the condition.

You can practice by yourself with the help of the C online compiler.

### What is the difference between strings in C and C++?

Strings in C are just char arrays that conclude with a NULL byte by convention. You can malloc the space for them in terms of dynamic memory management (including the extra byte). It is your responsibility to manage memory when altering strings: Strings (std::string) are objects in C++ that come with all of the related automatic memory management and control, making them much safer and easier to use, especially for beginners.

### How can I remove the leading spaces from a string?

The C programming language lacks a standard function for removing leading spaces from a string. It is, however, simple to create your function to accomplish this. To remove the leading spaces from a string, you may easily create a custom function that uses the rtrim() function with the standard C library function strrev().

### How can I copy just a portion of a string?

To copy a string section into another string, use the strncpy() method from the standard C library.

### How can I convert a number to a string?

The standard C library includes various functions for converting numbers of any type (integers, longs, floats, and so on) to strings and back. itoa(), one of these functions, is used to show how an integer is transformed into a string.

## Conclusion

In this article, we see the implementation of the C to count the number of vowels in a given string. We had also seen the output of the written program on some random input.