Queries to find the count of vowels in the substrings of the given string | C ++ implementation of the approach ; Function that returns true if ch is a vowel ; Function to return the count of vowels in the substring str [ l ... r ] ; To store the count of vowels ; For every character in the index range [ l , r ] ; If the current character is a vowel ; For every query ; Find the count of vowels for the current query ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  2 NEW_LINE bool isVowel ( char ch ) { return ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) ; } int countVowels ( string str , int l , int r ) { int cnt = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( isVowel ( str [ i ] ) ) cnt ++ ; } return cnt ; } void performQueries ( string str , int queries [ ] [ N ] , int q ) { for ( int i = 0 ; i < q ; i ++ ) { cout << countVowels ( str , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) << " STRNEWLINE " ; } } int main ( ) { string str = " geeksforgeeks " ; int queries [ ] [ N ] = { { 1 , 3 } , { 2 , 4 } , { 1 , 9 } } ; int q = ( sizeof ( queries ) / sizeof ( queries [ 0 ] ) ) ; performQueries ( str , queries , q ) ; return 0 ; }