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

C++ Write a C program to read a text file containing a list of vocabulary words

ID: 3919398 • Letter: C

Question

C++

Write a C program to read a text file containing a list of vocabulary words in alphabetical order. Your instructor will supply the word (text) file. The program should access the word list file and compute and display the following statistics:

a) total number of words are in the file (over 100,000 words)

b) # words begin with the letter 't'

c) # of words that have no vowels (a, e,i, o, u, y) Note: include ‘y’

d) average length of the words (use length() command)

e) minimum length of the words (shortest word length; display length and the shortest word)

f) maximum length of the words (longest word length; display length and the longest word)

h) # of words with all vowels (only vowels; a,e,i,o,u). May contain one or more vowels.

i) # of words that begin with “sh”

Notes:

1) The words.txt is a pure text (ASCII) file.

2) There are no definitions in the file, just words

3) There is one word per line in the file

4) There are 109,582 words in the file

5) The file size is over 1MB

6) Do not print out the file

7) The file can be opened in NotePad

8) Download this file from Canvas

9) Since I can't send the .txt file, if possible, make a .txt file with the sample text:

a
aah
aahed
aahing
aahs
aardvark
aardvarks
aardwolf
ab

Total number of words in this sample is 9.

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

bool hasVowel(string s);
bool noVowels(string s);
int main(){
ifstream infile;
string filename;

cout << "Enter input filename: ";
cin >> filename;

infile.open(filename.c_str());
if(infile.fail()){
cout << "ERROR: could not open input file " << filename << endl;
return 1;
}

string word;
long n = 0;
long begin_t = 0, no_vowels = 0, with_vowels = 0, begin_sh = 0;
int min_len = -1, max_len = 0;
long average_len = 0;
string shortest, longest;

while(infile >> word){
n++;
if(word[0] == 't')
begin_t++;

if(hasVowel(word))
with_vowels++;

if(noVowels(word))
no_vowels++;

if(word.find("sh") == 0)
begin_sh++;

average_len += word.length();

if(min_len == -1 || word.length() < min_len){
min_len = word.length();
shortest = word;
}

if(word.length() > max_len){
max_len = word.length();
longest = word;
}
}


infile.close();

cout << "Total number of words in this sample is " << n << endl;
cout << "Number of words which begin with letter t is " << begin_t << endl;
cout << "Number of words which begin with sh is " << begin_sh << endl;
cout << "Number of words without a, e, i, o, u, y is " << no_vowels << endl;
cout << "Number of words with vowels a, e, i, o, u is " << with_vowels << endl;
cout << "Minimum length word is " << shortest << " with length " << min_len << endl;
cout << "Maximum length word is " << longest << " with length " << max_len << endl;


}


bool hasVowel(string s){
for(int i = 0; i < s.length(); i++){
char c = s[i];
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c =='u')
return true;
}
return false;

}
bool noVowels(string s){
for(int i = 0; i < s.length(); i++){
char c = s[i];
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c =='u' || c == 'y')
return false;
}
return true;
}

output
------
Enter input filename: sample.txt
Total number of words in this sample is 9
Number of words which begin with letter t is 0
Number of words which begin with sh is 0
Number of words without a, e, i, o, u, y is 0
Number of words with vowels a, e, i, o, u is 9
Minimum length word is a with length 1
Maximum length word is aardvarks with length 9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote