To count Vowels in a string using Pointer in C++ Program


Finding the number of vowels in a string using pointers need you to understand string, vowels and how to use pointer with string.

String is an array of characters. And vowels are characters from the set {a,e,i,o,u}. Pointer is a variable that stores the value of memory location on a variable.

To find the number of vowels in a string. We will traverse the string and then compare each character with vowels and if it is equal then it increase a counter otherwise not.

Condition of the below code is that it requires a string that has all lowercase characters. If not you can use tolower() method.

Example

#include <iostream>
using namespace std;
int main() {
   char str[] = "i love tutorials point";
   char *prt ;
   prt = str;
   int count = 0;
   for(prt;(*prt) != '\0'; prt++) {
      if (*prt == 'a' || *prt == 'e' || *prt == 'i'|| *prt == 'o' || *prt == 'u') {
         count++;
      }
   }
   cout << "Vowels in the string: " << count;
   return 0;
}

Output

Vowels in the string: 9

Updated on: 08-Aug-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements