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

C++ (Count vowels and consonants, file input, nested-loops, switch statement) As

ID: 3776765 • Letter: C

Question

C++

(Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O and U are 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 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 file string. The while-loop will terminate when the end-of-file is reached. After the loop is finished, the program displays the total number of vowels and consonants in the text file.

[A text file, named “ass4_Q6_input.txt”, is provided as your testing input file.]

This what is going to be in the input file.

Explanation / Answer

#include<process.h>


#include <stdio.h>


        void main(int argc,char *argv[])


{


         


          FILE *filepointer1;


          int vowels=0,consonants=0;


          char characters;


          clrscr();


          if(argc!=2)


            {


                     printf("There are insufficient Arguments");


                     exit(0);


            }


          filepointer1=fopen(argv[1],"r");


          if(filepointer1==NULL)


            {


                    printf("This source is unable to be opened");


                    exit(0);


            }


          ch=fgetc(filepointer1);


          while(ch!=EOF)


                {


                      

if((ch=='a')||(ch=='A')||(ch=='e')||(ch=='E')||(ch=='i')||(ch=='I')||(ch=='o')

||(ch=='O')||(ch=='u')||(ch=='U'))


                         {


                               vowels++; // number of vowels incremented


                          }


                      else


                         {


                consonanst++; // number of consonant increented


                         }


                             ch=fgetc(filepointer1);


                }


                             printf(" The number of vowels are = %d",vowels);

                             printf(" The number of consonant are = %d",consonants);



                             getch();
}