-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1456-maxVowels.c
42 lines (37 loc) · 950 Bytes
/
1456-maxVowels.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isVowel (char c)
{
return (c == 'a' || c =='e' || c =='i' || c =='o' || c =='u') ;
}
int maxVowels(char* s, int k)
{
// count the vowels in the first substring of length k.
int currentVowels = 0 ;
int length = strlen(s) ;
for (int i = 0 ; i < k ; i++) {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'|| s[i] == 'o' || s[i] == 'u') {
currentVowels++ ;
}
}
int maxVowels = currentVowels ;
for (int i = k ; i < length ; i++) {
if (isVowel(s[i])) {
currentVowels++ ;
}
if (isVowel(s[i-k])) {
currentVowels-- ;
}
if (currentVowels > maxVowels) {
maxVowels = currentVowels ;
}
}
return maxVowels ;
}
int main ()
{
char s[] = "abciiidef" ;
int result = maxVowels(s, 3) ;
printf("The result is %d", result) ;
}