Write a program using c++ that analyzes the contents of a text file and produces
ID: 3714260 • Letter: W
Question
Write a program using c++ that analyzes the contents of a text file and produces the following results:
1) the number of words in the text file
2) the number of unique words in the text file
3) the number of characters in the text file
4) the number of occurrences of each lower case letter in the alphabet
5) the number of occurrences of each upper case letter in the alphabet
6) the frequency of each letter, both upper case and lower case, in the alphabet.
The program should prompt the user for the name of a text file and then analyze the text to generate the results described above. If the file does not exit, the program should report the error and give the user another try to enter another file name. If the number of tries exceeds 5, the program should display an appropriate error message and exits. Use functions to make the development process manageable and structure your code into multiple files. Make sure you document the functions you introduce by writing concise, precise and clear comments before you introduce the prototype of each function.
Explanation / Answer
count for number of words in text file
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
int main()
{
ifstream inFile; //Declares a file stream object
string fileName;
string word;
int count = 0;
inFile.open(fileName.c_str());
while(!inFile.eof())
{
inFile >> word;
count++;
}
cout << "Number of words in file is " << count;
inFile.close();
cin.get();
return 0;
}
-------------------------------------------------------------------------------------------------
Count for unique words in text file
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
using namespace std;
class MyCount
{
public:
int value;
MyCount() : value( 0 ) {}
void operator++ (int) { value++; }
};
ostream& operator<<(ostream& st, MyCount& wc )
{
return st << wc.value;
}
//You can add your path here
const string path = "C:\CheggFiles\mytext.txt";
//const string path = "/home/andy/NetBeansProjects/WordCount/Hamlet.txt"; //Linux
int main()
{
map<string, MyCount> counter;
ifstream input;
input.open( path.c_str() );
if ( !input )
{
cout << "Error in opening file Hamlet.txt ";
return 0;
}
string token;
while ( true )
{
input >> token;
if ( input )
{
counter[ token ]++;
}
else break;
}
map< string, MyCount,less<string> >::iterator it;
for ( it = counter.begin();
it != counter.end();
it++ )
{
cout << (*it).first
<< ", "
<< (*it).second
<< endl;
}
return 0;
}
-------------------------------------------------------------------------------------------------------------
count number of charecters in text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
int sum=0;
ifstream textData ;
textData.open("countletters.txt");
while(!textData.eof())
{
getline(textData,line);
int numofChars= line.length();
for (unsigned int n = 0; n<line.length();n++)
{
if (line.at(n) == ' ')
{
numofChars--;
}
}
sum=numofChars+sum;
}
cout << "Number of characters: "<< sum << endl;
return 0 ;
}
---------------------------------------------------------------------------------------------------------------
count number of upper and lower case letters
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstring>
#include <string>
using namespace std;
int main(){
string pav;
cout << "enter text file name (with .txt) : ";
cin >> pav;
ifstream file;
file.open(pav.c_str());
if (!file){
cout << "file not found." << endl;
system("pause");
}
file.close();
cout << "file " << pav << " opening successful " << endl;
cout << "--- text in the file --- ";
char str[20000];
file.open(pav.c_str());
while(!file.eof()){
file.getline(str,20000);
cout << str;
file.close();
}
cout <<endl << endl;
cout << "--- end of text--- ";
char ch;
int uppercase=0;
int lowercase=0;
file.open(pav.c_str());
while(!file.eof())
{
file.get(ch);
if ((ch >= 'A') && (ch <= 'Z'))
{
uppercase++;
}
else if ((ch >= 'a') && (ch <= 'z'))
{
lowercase++;
}}
}
---------------------------------------------------------------------------------------
frequency of letter and etc...
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int freq[128]; // frequencies of letters
ifstream inFile; // input file
char ch;
inFile.open("file.txt");
/* if (!inFile)
{
cout << "The input file could not be opened.";
return 1;
}
*/
// initialize frequency counts to zero for each possible letter
for (int k = 0; k < 128; k++)
{
freq[k] = 0;
}
// Read the file, keeping track of frequency with which each letter occurs
ch = inFile.get();
while (ch != EOF)
{
cout << ch;
ch = toupper(ch);
freq[ch]++;
ch = inFile.get();
}
// Print the output table
cout << endl << "Letter frequencies in this file are as follows." << endl;
for (char ch = 'A'; ch <= 'Z'; ch++)
{
cout << ch << " : " << freq[ch] << endl;
}
return 0;
}
--------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.