Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Count vowels and consonants, file input, nested-loops, switch statement) Assume

ID: 3683403 • Letter: #

Question

(Count vowels and consonants, file input, nested-loops, switch statement) Assume letters A, E, I, O, and U as the vowels. Write a program that reads strings from a text file, one line at a time, using a while-loop. You do the following operations within each loop: Read the one line from the input file and store it in a string; Count the number of vowels and consonants (using either while-loop or for-loop) in the string The while-loop will terminate when the end-of-file is reached. After the loop is finished, display the total number of vowels and consonants in the text file

Explanation / Answer

Solution for Question:

This below c program will count the number ovewels and consonents and special charcters .

1. Created the count variables will count the number vowels, consonents and charecters.

2. Read the data from file.

3. Will display at the end all of the counters and close the file pointer.

#include <stdio.h>
#include <stdlib.h> /* For exit() function*/
int main()
{
char ch;
int vowels =0, consonants = 0, special = 0;
FILE *fp;
if ((fp=fopen("data.txt","r"))==NULL){
printf("Error! opening file");
exit(1); /* Program exits if file pointer returns NULL. */
}

while( ( ch = fgetc(fp) ) != EOF )
{
   if ((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') || (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'))
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
if (ch =='t' ||ch =='' || ch ==' ')
{
special = special + 1;
}
}
consonants = consonants - special;
printf("No. of vowels are %d ", sentence, vowels);
printf("No. of consonants are = %d ", sentence, consonants);  

fclose(fp);
return 0;
}