Question

(Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O...

(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, is provided for testing input file.- It is basically a long 3 page questions attached as input file]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//////////////////////// c++ program

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(){
  
   // number of vowels
   // number of consonanats
  
   int vcount=0,ccount=0;
  
   ifstream file;
  
   cout<<"Enter file name : ";
   string st;
  
   cin>>st;
  
   //open file
  
   file.open(st.c_str());
  
   // if file not found
  
   if(!file.is_open()){
       cout<<"\n\nFile not found.";
   }
   else{
       string line;
      
       // read each line
      
       while(getline(file,line)){
          
           // get size of string
          
           int len=line.length();
          
           for(int i=0;i<len;i++){
              
               // read each character
               char ch=line.at(i);
              
              
               // check it is letter or not
              
               if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')){
              
              
                   // identify vowels
                   if(ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='I' || ch=='i' || ch=='o' || ch=='O' || ch=='u' || ch=='U'){
                       vcount++;
                   }
                  
                   // identify consonanats
                   else{
                       ccount++;
                   }
               }
           }
       }
      
       // output
      
       cout<<"Number of vowels are : "<<vcount;
       cout<<"\nNumber of consonants are : "<<ccount;
          
   }
system("pause");
   return(0);
  
}

////////////////////// java program


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;


public class JavaApplication47 {


public static void main(String[] args) {
System.out.print("Enter file name : ");
  
Scanner sc=new Scanner(System.in);
  
// get file name
String fileName=sc.next();
  
// number of vowels
// number of consonants
  
int vcount=0,ccount=0;
  
try {
BufferedReader br=new BufferedReader(new FileReader(new File(fileName)));
  
String line;
  
// read each line by line
while((line= br.readLine())!=null){
  
for(int i=0;i<line.length();i++){
  
char ch=line.charAt(i);
// check it is letter or not
  
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')){
  
  
// identify vowels
if(ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='I' || ch=='i' || ch=='o' || ch=='O' || ch=='u' || ch=='U'){
vcount++;
}
  
// identify consonanats
else{
ccount++;
}
}

}
}
  
// buffer close
  
br.close();
  
System.out.print("\nNumber of vowels are : "+vcount);
System.out.println("\nNumber of consonants are : "+ccount);
  
} catch (FileNotFoundException ex) {
System.out.println("File not found.");
}
catch (IOException ex) {   
System.out.println("Error in reading");
}
  
sc.close();
  
}
  
}

Add a comment
Know the answer?
Add Answer to:
(Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Count vowels and consonants) Assume letters A, E, I, O, and U as the vowels. Write...

    (Count vowels and consonants) Assume letters A, E, I, O, and U as the vowels. Write a program that prompts the user to enter a string and displays the number of vowels and consonants in the string. using python

  • Java: Assume letters A E O U I as the vowels. Write a program that prompts...

    Java: Assume letters A E O U I as the vowels. Write a program that prompts the user to enter a string and displays the number of vowels and consonants in the string. The same vowels or consonants are counted only once. Use sets in the code.

  • I need this in Visual Studio C++ Write a function that count the number of vowels,...

    I need this in Visual Studio C++ Write a function that count the number of vowels, the number of consonants and the average number of letters in each word. The function accept a C-String (char array) or a string object as an argument and return the number of vowels, number of consonants and the average number of letters in each word. Problem: Requirements: . Use pointers as part of the solution Read a string from a text file. . Write...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • In this assignment, you will write a program in C++ which uses files and nested loops...

    In this assignment, you will write a program in C++ which uses files and nested loops to create a file from the quiz grades entered by the user, then reads the grades from the file and calculates each student’s average grade and the average quiz grade for the class. Each student takes 6 quizzes (unknown number of students). Use a nested loop to write each student’s quiz grades to a file. Then read the data from the file in order...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • For this assignment, you will apply what you learned in analyzing for, while, and do-while loops...

    For this assignment, you will apply what you learned in analyzing for, while, and do-while loops by writing these statements yourself. The Java™ program you write should do the following: Display a pyramid of asterisks onscreen (i.e., a nested for loop) Display the integers 10 to 1 in decreasing order, one number per line (i.e., a while/do-whlie loop) Add 7 until the sum becomes greater than 157, at which point the program should display both the sum and the number...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT