Question

In: Computer Science

Query the user for the name of a file. Open the file, read it, and count...

Query the user for the name of a file. Open the file, read it, and count and report the number of vowels found in the file. Using C++.

Solutions

Expert Solution

C++ Program:

/* C++ Program that counts number of vowels in the file */

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

using namespace std;

//Main function
int main()
{
    char ch;
    string fileName;
    int vowelCount=0, aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0;

    //Reading file name
    cout << "\n Input file name: ";
    cin >> fileName;

    //Opening file in read mode
    fstream fin(fileName, ios::in);

    //Checking for file existence
    if(fin.fail())
    {
        //Printing error message
        cout << "\n Error!!! Cannot read input file... \n";
        return -1;
    }

    //Fetching data from file
    while(fin.good())
    {
        //Reading character from file
        fin >> ch;

        //Checking for character
        switch(ch)
        {
            //Vowel 'a'
            case 'a':
            case 'A':   aCnt++; vowelCount++; break;

            //Vowel 'e'
            case 'e':
            case 'E':   eCnt++; vowelCount++; break;

            //Vowel 'i'
            case 'i':
            case 'I':   iCnt++; vowelCount++; break;

            //Vowel 'o'
            case 'o':
            case 'O':   oCnt++; vowelCount++; break;

            //Vowel 'u'
            case 'u':
            case 'U':   uCnt++; vowelCount++; break;

            default:    break;
        }
    }

    //Printing final report
    cout << "\n\n Total number of Vowels: " << vowelCount;
    cout << "\n\n Vowel 'A': " << aCnt;
    cout << "\n\n Vowel 'E': " << eCnt;
    cout << "\n\n Vowel 'I': " << iCnt;
    cout << "\n\n Vowel 'O': " << oCnt;
    cout << "\n\n Vowel 'U': " << uCnt;

    cout << "\n\n";
    return 0;
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:


Related Solutions

Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
Please Use Python 3.The Problem: Read the name of a file and then open it for...
Please Use Python 3.The Problem: Read the name of a file and then open it for reading. Read the name of another file and then open it for output. When the program completes, the output file must contain the lines in the input file, but in the reverse order. • Write the input, output, and the relationship between the input and output. • Develop the test data (A single test will be alright). Make the input file at least 3...
Write a MIPS Assembly language program to request a file name from the user, open the...
Write a MIPS Assembly language program to request a file name from the user, open the file, read the contents, and write out the contents to the console. This is what I have so far: .data    fileName:   .space 100    prompt1:   .asciiz "Enter the file name: "    prompt2:    .asciiz ""    prompt3:   .asciiz "\n"    buffer:    .space 4096 .text    main:        #        li $v0, 4        la $a0, prompt1       ...
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a program that prompts the user for a file name, make sure the file exists...
Write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try again (hint:...
Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
c++ In this program ask the user what name they prefer for their file, and make...
c++ In this program ask the user what name they prefer for their file, and make a file, the file name should za file. Get a number from the user and save it into the create file. should be more than 20 number in any order. print out all 20 numbers in a sorted array by using for loop, last print out its total and average. At last create a function in which you will call it from main and...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT