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

c++ I am puzzled on how to read an input file by line then count the characters

ID: 3877058 • Letter: C

Question

c++

I am puzzled on how to read an input file by line then count the characters a-z (either uppercase or lowercase) and then writing the char and count to an output file. I need to cycle through the input file until the end of file. The formal directions are " the function first reads the paragraph string from the input file stream, then counts the letter frequencies of that paragraph, stores the frequencies in the array and outstreams the letter frequencies after each paragraph in the file to another file(txt). Disregards upper/lower case letters." this function will take in a file stream array and a pointer to an array of integers.

Explanation / Answer

#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <fstream>
#include <ctype.h>


using namespace std;


int *charCount(vector<string> &strVector){
int *frequency = new int[26];
  
// Intialize array with 0
int j = 0;
for(; j < 26; j++){
frequency[j] = 0;
}
  
vector<string>::iterator itrL;
for ( itrL = strVector.begin(); itrL != strVector.end(); ++itrL ) {
string str = itrL->c_str();
char c;
int i = 0;
for(; i < str.size(); i++)
{
c=str[i];
int asciiValue = int(toupper(c));
if(asciiValue >= 65 && asciiValue <= 90){
frequency[asciiValue-65] = frequency[asciiValue-65] + 1;
//cout << asciiValue << " ---- " << frequency[asciiValue-65] << endl;
}else{
// cout << "Spacial character case " << endl;
}
}
}
return frequency;
}


int main()
{
  
std::ifstream file("words.txt");
std::string line;
std::vector<string> tokens;
// push all the file lines in one vector
while(std::getline(file, line)) {
//cout << " line " << line <<endl;
tokens.push_back(line);
}

ofstream outputFile;
outputFile.open("output.txt");
vector<string>::iterator it1;
std::vector<string> paragraph;
int *fre = new int[26];
// intialize array with 0 value.
int j = 0;
for(; j < 26; j++){
fre[j] = 0;
}
for ( it1 = tokens.begin(); it1 != tokens.end(); ++it1 ) {
string str = it1->c_str();
if(!str.empty()){ // non empty line
paragraph.push_back(str);
cout << endl << str ;
outputFile << str << endl;
if(it1 == tokens.end()-1){
// cout << " End of File case" << endl;
int *fre2 = charCount(paragraph);
int indx = 0;
for(; indx < 26 ; indx++){
fre[indx] = fre[indx] + fre2[indx];
cout << char (indx +65 ) << " " << fre[indx] << " " ;
outputFile << char (indx +65 ) << " " << fre[indx] << " " ;   
}
cout << endl ;
}
}else{ // empty line case where paragraph ends.
int *fre1 = charCount(paragraph);
int indx = 0;
for(; indx < 26 ; indx++){
fre[indx] = fre[indx] + fre1[indx];
cout << char (indx +65 ) << " " << fre[indx] << " " ;
outputFile << char (indx +65 ) << " " << fre[indx] << " " ;
}
paragraph.erase (paragraph.begin(),paragraph.end());
outputFile << endl;   
}
}
outputFile.close();
return 0;
}

Input

-------

This is my computer.
I developed one app.

Hi I am same.
How are you ?

Output

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

This is my computer.
I developed one app.
A 1 B 0 C 1 D 2 E 5 F 0 G 0 H 1 I 3 J 0
K 0 L 1 M 2 N 1 O 3 P 4 Q 0 R 1 S 2 T 2
U 1 V 1 W 0 X 0 Y 1 Z 0
Hi I am same.
How are you ?   
A 4 B 0 C 1 D 2 E 7 F 0 G 0 H 3 I 5 J 0
K 0 L 1 M 4 N 1 O 5 P 4 Q 0 R 2 S 3 T 2  

Description

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

1. Program first read or caprtured all the file line in one single main string vector.

2. now program read the main vector one by by till the empty line not found. once empty line found , process cout the char frequency and write the paragraph conetent as well as frequencies for all the characters( A-Z )

3. program ignore the cases means a-z is equals to [A-Z] and special characters.

4. i have introduced one method for character counting. mehtod retruns a pointer of integer array.

Please chekc the above code and let me know if you found any difficulties.

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