C++ program to count the number of vowels in a string

Counting the number of vowels in a string.

In this article we will learn how to write a C++ program to count the number of vowels in a string. First we will accept the string then we will iterate each character of the string through a for loop and if the character iterated is found to be a vowel (i.e. ‘A’, ‘E’, ‘I’, ‘O’, ‘U’) then we will increase the count of vowel by one. We will have to check for both upper case and lower case at every iteration as C++ programming is case sensitive.
 

C++ program to count the number of vowels in a string

Algorithm:

  • Initialize the variable.
  • Accept the input.
  • Initialize for loop and terminate it at the end of string.
  • Check and count the number of vowels in a string.
  • Print total count of vowels.

C++ program to count the number of vowels in a string

Competitive Coding Techniques
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    //Initializing variable.
    char str[100];  
    int i,vowels=0;
    
    //Accepting input.
    cout<<"Enter  the string : ";
    gets(str);
    
    //Initializing for loop. 
    for(i=0;str[i];i++)  
    {
        //Counting the vowels.
        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')
        {
		    vowels++;
        }
    }
 	
    //Printing the count of vowels.
    cout<<"Total number of vowels in the string = "<<vowels;
    
    
    return 0;
}
Output:
Enter the string : PREPINSTA
Total number of vowels in the string = 3

Operations on Strings

  • Length of the string without using strlen() function : C | C++ | Java | Python
  • Toggle each character in a string : C | C++ | Java | Python
  • Count the number of vowels : C | C++ | Java | Python
  • Remove the vowels from a String : C | C++ | Java | Python
  • Check if the given string is Palindrome or not : C | C++ | Java | Python
  • Print the given string in reverse order : C | C++ | Java | Python
  • Remove all characters from string except alphabets : C | C++ | Java | Python
  • Remove spaces from a string : C | C++ | Java | Python
  • Remove brackets from an algebraic expression : C | C++ | Java | Python
  • Count the sum of numbers in a string : C | C++ | Java | Python
  • Capitalize the first and last character of each word of a string : C | C++ | Java | Python
  • Calculate frequency of characters in a string : C | C++ | Java | Python
  • Find non-repeating characters in a string : C | C++ | JAVA | Python
  • Check if two strings are Anagram or not : C | C++ | Java | Python
  • Replace a sub-string in a string: C | C++ | Java | Python
  • Count common sub-sequence in two strings : C | C++ | Java | Python
  • Check if two strings match where one string contains wildcard characters : C | C++ | Java | Python
Operations on Strings