1.
Introduction
2.
Problem Statement
2.1.
Idea
2.2.
Algorithm
2.3.
Implementation
2.4.
Program Explanation
3.
3.1.
What is the difference between strings in C and C++?
3.2.
How can I remove the leading spaces from a string?
3.3.
How can I copy just a portion of a string?
3.4.
How can I convert a number to a string?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

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

0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## 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 and C Static Function.

## 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:

You can also read about dynamic array in c and Tribonacci Series.

### 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

### 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.

Bubble Sort Program in C

Ternary Operator in C

Conditional Operator in C

Reverse A String In C

Getch in C

Short int in C Programming

Recommended problems -

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass