/*--------------------------------------------------------------------*/ /* Problem chapter6_23 */ /* */ /* This program reads a data file and determines the number of */ /* occurances of each character in the file. It then prints the */ /* characters and the number of times that they occurred. */ /*--------------------------------------------------------------------*/ #include #include #include using namespace std; int main() { /* Declare variables. */ int occur[128], i; char c; ifstream fin; string filename; /* Open input file. */ cout << "Enter name of input file: "; cin >> filename; fin.open(filename.c_str() ); if(fin.fail()) { cerr << "error opening file " << filename << endl; exit(1); } /* Initialize array. */ for (i = 0; i<128; i++) occur[i] = 0; /* Set the array to be 0 */ /* Count characters */ fin.get(c); while( !fin.eof() ) { occur[(int)c]++; fin.get(c); } /* Output the results. */ for (i = 0; i<128; i++) { if (occur[i] != 0) cout << "Character " << (char)i << " OCCURANCES: " << occur[i] << endl; } fin.close(); /* Exit program. */ return 0; }