#include <stdio.h>
int vowel_count(char n[]){
int hasil = 0;
char vowel[] = "aiueoyAIUEOY";
for (int i = 0; i < 50; i++)
{
for (int x = 0; x < 12; x++)
{
if (n[i] == vowel[x])
{
hasil++;
}
}
}
return hasil;
}
int main(void){
int amount;
char values[50], unknown[10];
char vowel[] = "AIUEOYaiueoy";
FILE* fp = fopen("zValues.txt", "r");
fscanf(fp, "%d", &amount);
fgets(unknown, 10, fp);
for (int n = 0; n < amount; n++)
{
fgets(values, 50, fp);
printf("%d ", vowel_count(values));
}
fclose(fp);
}
here is the zValues.txt:
5
abracadabra
pear tree
o a kak ushakov lil vo kashu kakao
my pyx
riszky hermawan
when i run the code,it shows:
5 4 13 12 12
see the problem? it's wrong answer" ouput must be like this
5 4 13 2 5
Since your code uses the function, fgets to read the file content, the function
vowel_count
should not iterate over50
array characters. Some of the lines (read from the file) may be of different length. Consequently, iterating beyond50
characters may fetch random values from memory, which may include vowels.Therefore you just need to adapt the function
vowel_count
, namely change:to
Moreover, IMO it is better to do :
instead of
You do not need to hardcode the size of the array because when you write
char vowel[] = "aiueoyAIUEOY"
, the terminal character (i.e.'\0'
) gets added automatically at the end of it. Although in your case is not very problematic, because the number of vowels will probably remain the same, in other cases, it is prone to bugs.