#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

int main(int argc, char **argv){
  using std::cout;
  using std::string;
  int charCount = 0, wordCount = 0, lineCount = 0;
  bool doChar = false, doWord = false, doLine = false, inWord = false;
  char c;
  std::istream *source = &std::cin;
  string fileName;
  std::ifstream filestream;
  argv++;
  while (0 != --argc) {
    if (string("-c") == *argv) { doChar = true;
    } else if (string("-w") == *argv) { doWord = true;
    } else if (string("-l") == *argv) { doLine = true;
    } else {
      fileName = *argv;
      filestream.open(fileName);
      if (filestream.good()) {
        source = &filestream;
      } else {
        cout << "Usage: wc [-l] [-w] [-c] [{fileName}]\n";
        return 1;
      }
    }
    argv++;
  }
  if (!(doChar || doWord || doLine)) doChar = doWord = doLine = true;
  // Done with command line processing. Now doing real work.
  while (!source->eof()){
    source->get(c);
    charCount++;
    if (c == '\n') { lineCount++; }
    if (!isspace(c)) {
      if (!inWord) {
        inWord = true; wordCount++;
      }
    } else {
      inWord = false;
    }
  }
  // Show the results of the work.
  if (doLine) { cout << lineCount << "\t"; }
  if (doWord) { cout << wordCount << "\t"; }
  if (doChar) { cout << charCount << "\t"; }
  if (fileName.size()) { cout << fileName; }
  cout << std::endl;
  return 0;
}
