letters.cc From Astrachan, pp 369-370 ------------------------------------- #include #include // for ifstream #include // for exit() #include // for tolower() #include // for CHAR_MAX #include "CPstring.h" #include "prompt.h" #include "vector.h" // count # occurrences of all characters in a file // written: 8/5/94 // by: Owen Astrachan void Print(const Vector & counts, int total); void Count(istream & input, Vector & counts, int & total); int main() { int totalAlph = 0; string filename = PromptString("enter name of input file: "); ifstream input(filename); if (input.fail() ) { cout << "could not open file " << filename << endl; exit(1); } Vector charCounts(CHAR_MAX+1,0); // all initialized to 0 Count(input,charCounts,totalAlph); Print(charCounts,totalAlph); return 0; } void Count(istream & input, Vector & counts, int & total) // precondition: input open for reading // counts[k] == 0, 0 <= k < CHAR_MAX // postcondition: counts[k] = # occurrences of character k // total = # alphabetic characters { char ch; while (input.get(ch)) // read a character { if (isalpha(ch)) // is alphabetic (a-z)? { total++; } ch = tolower(ch); // convert to lower case counts[ch]++; // count all characters } } void Print(const Vector & counts, int total) // postcondition: all values of counts from 'a' to 'z' printed { cout.setf(ios::fixed); // print 1 decimal place cout.precision(1); char k; for(k = 'a'; k <= 'z'; k++) { cout << k; cout.width(7); cout << counts[int(k)] << " "; cout.width(4); cout << 100 * double(counts[int(k)])/total << "%" << endl; } } Sample output: letters enter name of input file: letters.cc [a C++ program] a 71 7.0% b 5 0.5% c 86 8.5% d 28 2.8% e 72 7.1% f 25 2.5% g 5 0.5% h 37 3.6% i 86 8.5% j 0 0.0% k 11 1.1% l 53 5.2% m 19 1.9% n 85 8.4% o 89 8.8% p 32 3.2% q 0 0.0% r 64 6.3% s 41 4.0% t 125 12.3% u 48 4.7% v 12 1.2% w 8 0.8% x 6 0.6% y 2 0.2% z 4 0.4% letters enter name of input file: plan.tex [an English document] a 4013 7.8% b 552 1.1% c 2114 4.1% d 1885 3.6% e 6517 12.6% f 1185 2.3% g 1178 2.3% h 1808 3.5% i 3870 7.5% j 75 0.1% k 254 0.5% l 1882 3.6% m 1566 3.0% n 3735 7.2% o 3328 6.4% p 1581 3.1% q 82 0.2% r 3782 7.3% s 3550 6.9% t 4784 9.3% u 1876 3.6% v 552 1.1% w 617 1.2% x 118 0.2% y 720 1.4% z 51 0.1%