1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
void initialize (int& lc, int list[]);
void copyText (ifstream& intext, ofstream& outtext, char& ch, int list[]);
void characterCount (char ch, int list[]);
void writeTotal (ofstream& outtext, int lc, int list[]);
int main()
{
//Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;
infile.open("textin.txt");
if (!infile)
{
cout << "Cannot open the input file." << endl;
return 1;
}
outfile.open("textout.txt");
initialize(lineCount, letterCount);
infile.get(ch);
while (infile)
{
copyText(infile, outfile, ch, letterCount);
lineCount++;
infile.get(ch);
}
writeTotal(outfile, lineCount, letterCount);
infile.close();
outfile.close();
return 0;
}
void initialize (int& lc, int list[])
{
int j;
lc = 0;
for (j = 0; j < 26; j++)
list[j] = 0;
}
void copyText (ifstream& intext, ofstream& outtext, char& ch, int list[])
{
while (ch != '\n')
{
outtext << ch;
characterCount (ch, list);
intext.get(ch);
}
outtext << ch;
}
void characterCount (char ch, int list[])
{
int index;
ch = toupper(ch);
index = static_cast<int>(ch) - static_cast<int>('A');
if (0 <= index && index < 26)
list [index]++;
}
void writeTotal (ofstream& outtext, int lc, int list[])
{
int index;
outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;
for (index = 0; index < 26; index++)
outtext << static_cast<char>(index + static_cast<int>('A')) << "count = " << list[index] << endl;
}
|