/********************************************************/ // R. A. Hillyard // charCount.cpp // October 2001 // // // read character from a file and count occurences of each letter // and number of other non alphabet symbols. Then reports the //result to the user /********************************************************/ #include<iostream> //cout and cin #include<iomanip> //setw #include<fstream> //file functions #include<cctype> //character functions #include<cstdlib> //for exit using namespace std; bool openInFile(ifstream& inData); //pre conditions: inData has been declared as a file streams //post condition: inData will have been opened - function returns false on fail void printArray(int a[], int arraySize); //pre conditions: a[] has valid data for size elements //post condition: Will display a character count to the screen void initArray(int a[], int size); //pre conditions: a[] and size have been declared //post condition: each element of a[] will be initialized to 0 const int SIZE = 27; //size or alphabet array int main() { int charCount[SIZE]; //array to hold letter counts char next; //next character in the file int index; //index for array element ifstream inData; //declare file streams if(!openInFile(inData)) //open file for read - check for fail { exit(1); } initArray(charCount, SIZE); //initialize array counts inData.get(next); //Read data from file a character at a time while(!inData.eof()) //stop at end of file { //cout << next; if(isalpha(next)) //if alphabet char increment count { next = toupper(next); //treat lower and upper case the same index = next - 'A'; //ASCII Value gives offset into array charCount[index]++; //update that counter } else //update other count. { if(!isspace(next)) //skip white space charCount[SIZE-1]++; } inData.get(next); //get next character from file }//end while //report results printArray(charCount,SIZE); }//end main /*************************************************************/ //Initialize each element of an integer array to 0 /*************************************************************/ void initArray(int a[], int size) { for(int i = 0; i<size; i++) { a[i] = 0; } } /*************************************************************/ //print elements of array - formatted to appear in columns /*************************************************************/ void printArray(int a[], int size) { cout << "\nThe file contains the following letter counts\n"; for(int i = 0; i<size-1; i++) { cout << setw(4) << char('A' + i) << " - " << setw(3) << a[i]; if((i+1)%5 == 0) cout << endl; } cout << "\nThere were " << a[size-1] << " other symbols\n"; } /*************************************************************/ /*************************************************************/ bool openInFile(ifstream& inData) { char name[20]; cout << "Enter File Name: "; cin >> name; inData.open(name); if(inData.fail()) { cout << "Failed to open input file " << name << endl; return false; } return true; }//end openInFile /*************************************************************/
/******************Input File***************** This is the hello program it has all kinds of characters in it. There are digits such as: 2,5 ,789, 3456 and last but not least numbers like 45.78 and 33.56. Special ^%# charac)(?<>ters abound!!!! + u get 2 Count spaces * dont forget a tab to add + sub - or *.,~ You can test your program by getting the following results Total chars: 448 Total white: 107 Total digit: 33 Total letter: 274 Total vowel: 99 Total Other: 34
/******************Program Output*************
Enter File Name: letterCount.txt The file contains the following letter counts A - 28 B - 6 C - 10 D - 9 E - 23 F - 3 G - 9 H - 12 I - 15 J - 0 K - 2 L - 19 M - 3 N - 12 O - 23 P - 4 Q - 0 R - 18 S - 20 T - 41 U - 10 V - 1 W - 3 X - 0 Y - 3 Z - 0 There were 67 other symbols */