Write a function numVowels that takes a string representing

Write a function numVowels that takes a string representing a file name as an argument and returns the number of vowels (both upper- and lowercase) that appear in the file. For the purpose of this question a vowel is one of a, e, i, o, or u. The following shows how the function would behave for several sample files that are included with the assignment:

>>> numVowels(\'vowels.txt\')

21

>>> numVowels(\'poppins.txt\')

418

>>> numVowels(\'empty.txt\')

0

>>>

Solution

#include<stdio.h>

#include<stdlib.h> //For exit() function
FILE *file;
//Returns number of vowel
int numVowels(char fname[])
{
char c;
int count = 0;
//Opens a file passed as parameter
file = fopen(fname, \"r\");
//If file exists
if (file)
{
//Reads till end of file
while ((c = getc(file)) != EOF)
{
//Displays each character
putchar(c);
//Checks the vowel
if(c == \'a\'|| c == \'A\' || c == \'e\'|| c == \'E\' || c == \'i\'|| c == \'I\' || c == \'o\'|| c == \'O\' || c == \'u\'|| c == \'U\')
//Counts vowel
count++;
}
//Close file
fclose(file);
}
//Error if file not available
else
{
printf(\"\ Error: Unable to open the file\");

  exit(0);
}
return count;
}
int main()
{
char fn[10];
int res;
//Accepts a file name
printf(\"\ Enter File Name: \");
scanf(\"%s\", fn);
//Displays number of vowels
printf(\"\ Number of vowel present in the file: %d\", numVowels(fn));
}

Output 1:

Enter File Name: input.txt
This is a demo.
For all files to check.
Number of vowel present in the file: 11

Output 2:

Enter File Name: check.txt
My program test.
Checks the vowl.
Number of vowel present in the file: 6

Output 3:

Enter File Name: myfile.txt

Error: Unable to open the file

Write a function numVowels that takes a string representing a file name as an argument and returns the number of vowels (both upper- and lowercase) that appear
Write a function numVowels that takes a string representing a file name as an argument and returns the number of vowels (both upper- and lowercase) that appear

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site