// occurrences.cpp: a silly program to count the number of times a character // appears in the input where the target is specified on the command line // The default character is a semicolon (a rough count of statements in // a C++ program). // Build with g++ -std=c++14 occurrences.cpp #include using namespace std; int countOf(const char *strp, char target) { if ( strp == nullptr ) return 0; int count = 0; for(; *strp; ++strp) // *strp is equivalent to *strp != '\0' if ( *strp == target ) ++count; return count; } int main(int argc, char **argv) { char target = ';'; if ( argc > 1 && argv[1] != nullptr ) target = argv[1][0]; int total_count = 0; char line[1000]; cin >> line; while ( cin ) { total_count += countOf(line, target); cin >> line; } cout << "Total count of '" << target << "': " << total_count << endl; return 0; }