Creat a file that contains words in the US dictionary (about 118,000 words – all
ID: 3768421 • Letter: C
Question
Creat a file that contains words in the US dictionary (about 118,000 words – all in lower case).
Write a C++ program that will determine and display the following items:
The total number of words in the dictionary.
The total number of characters in the dictionary (not including white spaces)
The total number of characters in the dictionary (including white spaces) – Hint:Use get( ).
The total number of words ending in the letter b.
The total number of words ending in the letter t.
The total number of 5 letter words.
The total number of words ending with a vowel.
The total number of words containing the substring “est”.
The total number of occurances of the letter t.
The total number of words containing at least two occurances of the letter t.
The total number of words containing 3 or more vowels.
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream file("wordcount.txt");
string s1;
static int b_count=0,end_t_count=0,size_5count=0,end_vowelCount=0,est_count=0,t_count=0,final_tcount=0,final_vowelcount=0;
int temp_vowel_count,temp_t_count;
int i;
int words=0, lines=0, ch_spaces=0, ch_no_spaces=0, spaces=0;
while (getline(file, s1))
{
ch_spaces += s1.size();
lines++;
for (int i=0; i<s1.size(); i++)
{
if (isspace(s1[i]))
spaces++;
}
}
file.clear();
file.seekg(0, ios::beg);
while (file >> s1)
{
words++;
if(s1[s1.size()-1]=='b')
b_count++;
if(s1[s1.size()-1]=='t')
end_t_count++;
if(s1.size()==5)
size_5count++;
if(s1[s1.size()-1]=='a' ||s1[s1.size()-1]=='e'||s1[s1.size()-1]=='i'||s1[s1.size()-1]=='o'||s1[s1.size()-1]=='u')
end_vowelCount++;
for(i=0;i<s1.size();i++)
{if(s1[i]=='e' && s1[i+1]=='s'&&s1[i+2]=='t')
est_count++;
}
for(i=0;i<s1.size();i++)
{if(s1[i]=='t')
t_count++;
}
}
file.clear();
file.seekg(0, ios::beg);
while (file >> s1)
{ temp_t_count=0;
temp_vowel_count=0;
for(i=0;i<s1.size();i++)
{if(s1[i]=='t')
temp_t_count++;
}
if(temp_t_count>=2)
final_tcount++;
for(i=0;i<s1.size();i++)
{if(s1[i]=='a' ||s1[i]=='e'||s1[i]=='i'||s1[i]=='o'||s1[i]=='u')
temp_vowel_count++;
}
if(temp_vowel_count>=3)
final_vowelcount++;
}
ch_no_spaces = ch_spaces - spaces;
cout << "The file contains " << words << " words. ";
cout << "The file contains " << ch_no_spaces << " characters (excluding spaces) ";
cout << "The file contains " << ch_spaces << " characters (including spaces) ";
cout << "Total no of words ending with letter 'b' " << b_count <<endl;
cout << "Total no of words ending with letter 't' " << end_t_count <<endl;
cout << "Total 5 letter words " <<size_5count<<endl;
cout << "Total no of words ending with vowel " <<end_vowelCount<<endl;
cout << "Total no of words containing est as substrng " <<est_count<<endl;
cout << "The total number of occurances of the letter t. " <<t_count<<endl;
cout << "The total number of words containing at least two occurances of the letter t. " <<final_tcount<<endl;
cout << "The total number of words containing 3 or more vowels " <<final_vowelcount<<endl;
system("pause");
return 0;
}