/* C++ Programming, Example answer, Exercise 5, Sheet 4  */

/* Author: William Knottenbelt
   Program last changed: 30 September 2023    */
   
/* This program prints a table of character occurrences 
   in the file "Sheet4Ex5.cpp"                            */

int count_character(char letter);

#include <iostream>
#include <fstream>
using namespace std;

int main() 
{
  char letter;

  cout.setf(ios::left);

  /* Print table heading */
  cout.width(19);
  cout << "CHARACTER";

  cout << "OCCURRENCES" << endl << endl;

  /* print table of characters */
  for (letter = 'a' ; letter <= 'z' ; letter++) {
    cout.width(19);
    cout << letter;
    cout << count_character(letter) << endl;
  }
	
  return 0;
}

int count_character(char letter) {

  char character;
  ifstream in_stream;

  in_stream.open("Sheet4Ex5.cpp");
  in_stream.get(character);
		
  int count = 0;
  while (! in_stream.fail()) {
    if (character == letter)
      count++;
    in_stream.get(character);
  }
  in_stream.close();

  return count;
}
