Count of substrings consisting only of vowels | C ++ program to Count all substrings in a string which contains only vowels ; Function to check vowel or not ; Function to Count all substrings in a string which contains only vowels ; Check if current character is vowel ; Increment length of substring ; Calculate possible substrings of calculated length ; Reset the length ; Add remaining possible substrings consisting of vowels occupying last indices of the string ; Driver Program
#include <iostream> NEW_LINE using namespace std ; bool isvowel ( char ch ) { return ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) ; } int CountTotal ( string & s ) { int ans = 0 ; int n = s . length ( ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isvowel ( s [ i ] ) ) cnt += 1 ; else { ans += ( cnt * ( cnt + 1 ) / 2 ) ; cnt = 0 ; } } if ( cnt != 0 ) { ans += ( cnt * ( cnt + 1 ) / 2 ) ; } return ans ; } int main ( ) { string s = " geeksforgeeks " ; cout << ( CountTotal ( s ) ) << endl ; return 0 ; }