Character Count Assignment

For this assignment you will write a program that counts the number of times a target character appears in some input. The character to search for will be specified as a command line argument. The input text could either be a command line argument or standard input.

Write the entire program in the provided file, char_count.c.

main()

In the past, our main() function has had no parameters. Since we want to use the command line arguments in this program we will need to add two parameters to the function:

int main(int argc, char **argv) {
    ...

Program Behavior

If you compiled the program and named the executable char_count, then you must be able to run the program like so:

./char_count h hello
1

The program prints out 1, because there is 1 'h' in "hello".

You must also be able to run your program so that it searches through standard input to count occurrences like this:

./char_count h
hello, how are you?
2

When reading from standard input you will read every character until you get to EOF. Remember that you need to press ctrl+d in the terminal to send an EOF. You may see a ā€œDā€ along with the printed number as a result.

I recommend getting it to work with either command line input or standard input first, and then getting it to work with both.

The command line arguments are passed into main() by the operating system through the argv parameter. argc is the number of arguments. If you run the program as shown in the first example, then argc will be 3, argv[0] will be "./char_count", argv[1] will be "h", and argv[2] will be "hello".

argv[1] contains the target character, but it is a string not a single character. To access the character itself, get the first character of argv[1] using argv[1][0].

Input Validation

If the program is called incorrectly, you must detect the input error, print a message describing what is wrong, and return a non-zero exit code. The exit code is simply the value that main() returns. So far we have had our programs always return 0, but here we will return different values based on different error conditions.

If the program is run correctly there will be either 2 or 3 arguments, so argc must be 2 or 3. If argc has any other value, print a message about how to correctly use the program and return 1.

The user might also pass a string as the target that has more than 1 character in it. Check to be sure argv[1] has a length of 1. If it does not, print a message indicating what is wrong and return 2.

Example Output

Here are some example runs of the program, and the expected output.

./char_count h hello
1
./char_count
Usage:
Counting from standard input: ./char_count <character>
Counting from the command line: ./char_count <character> <input string>

Normally spaces separate command line arguments. If you want to use an argument that contains spaces, you can use quotes:

./char_count b "big brown bear"
3
./char_count e
command line arguments are great
4
./char_count a too many arguments
Usage:
Counting from standard input: ./char_count <character>
Counting from the command line: ./char_count <character> <input string>
$ ./char_count hello hello
The <character> argument must contain a single character

Submission

Push your code to git-keeper. The tests will make sure that your program prints the correct output when given valid arguments, and that the error code is correct when passed incorrect arguments.

In addition, you will be graded on proper style.