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

You are provided a file HW7Prob1.dat with text on it. Initialize an array of cha

ID: 3815973 • Letter: Y

Question

You are provided a file HW7Prob1.dat with text on it. Initialize an array of character counters with 5 array elements i.e. 5 counters for counting the following i) All digits (0-9) ii) All lower case alphabets (a-z) iii) All uppercase alphabets (A-Z) iv) Number of spaces v) All other remaining characters After reading the file and counting the number of characters, display the value of each counter on the screen. Remember, you cannot have individual variables for counters, they MUST be elements on an array.

Explanation / Answer

#include<iostream>
#include<fstream>

using namespace std;

int main() {
int counters[5] = {0,0,0,0,0};
ifstream inputFile;
inputFile.open("HW7Prob1.dat");
string line;
if (inputFile.is_open()) {

while (getline(inputFile, line)){
for(int i=0; i<line.length() ;i++){
if(line[i]>='0' && line[i]<='9'){
counters[0]++;
}
else if(line[i]>='a' && line[i]<='z'){
counters[1]++;
}
else if(line[i]>='A' && line[i]<='Z'){
counters[2]++;
}
else if(line[i]==' '){
counters[3]++;
}
else{
counters[4]++;
}
}


}
}
cout<<"Number of digits: "<<counters[0]<<endl;
cout<<"Number of lower letters: "<<counters[1]<<endl;
cout<<"Number of upper letters: "<<counters[2]<<endl;
cout<<"Number of white spaces: "<<counters[3]<<endl;
cout<<"Number of other characters: "<<counters[4]<<endl;

inputFile.close();
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Number of digits: 10                                                                                                                                                                                                                                                   

Number of lower letters: 23                                                                                                                                                                                                                                            

Number of upper letters: 6                                                                                                                                                                                                                                             

Number of white spaces: 3                                                                                                                                                                                                                                              

Number of other characters: 13

HW7Prob1.dat

acdQ
12 asdaWs
3 sa
d s####
sdsa@@#!!!
g
hh!@#
ZX
ZX
vd
qq222
4
5
6
7

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