Getting started with C

Starter code: starting-c-package.tar
Upload solutions via Moodle as: starting-c.tar

Goals

Rubric

1 - author name(s) in comment at top of each C source file 3 - "depunctuate" program correctness 6 - "sorter" program correctness 2 - code quality

How I learn new languages

Once you have learned a couple programming languages, getting started in a new language is mostly a matter of finding good reference materials, getting a source of sample programs, and writing a bunch of small programs to get the syntax and core libraries under control.

When I want to learn a new programming language, I start by writing a few small programs to make sure I can handle the basics. Sometimes, I'll just do the first 10 problems at Project Euler. But usually, I prefer to ramp up faster by writing programs specifically aimed at teaching myself key elements of the new language. For example:

After getting these basics under control, I start exploring the language's standard libraries. I will always want to know more about string manipulation, the file system (create/delete/move files, traverse a directory tree, etc.), simple GUIs and line graphics, invoking other programs, networking, etc. But this is a longer-term project. If I have a good personal project to work on, most of these libraries come up naturally.

Your assignment

Write C versions of the files/loops/command-line and lists/arrays programs described above.

More specifically:

  1. depunctuate.c. This program's command-line syntax will be:

    ./depunctuate inputfile outputfile

    The program will copy the contents of inputfile to outputfile unchanged except that only digits (ASCII 48-57), tabs and spaces (9, 32), newlines (10, 13) and letters (65-90 uppercase, 97-122 lowercase) will be written to the output file.

  2. sorter.c. This program's command-line syntax will be:

    ./sorter textfile

    The program will read the lines of the text file into an array, sort them lexicographically (i.e. using the return value of strcmp or strncmp as the comparison function), and print the sorted list to standard output. You may assume that:

    • the input file is an ASCII file; that is, every byte in the file has a value between 0 and 127
    • lines are delimited by the newline char '\n' (ASCII 10), except possibly for the last line in the file, which may or may not end in '\n'
    • no line contains more than 200 bytes, including the '\n'
    • there are no more than 500 lines in the input file
    • it's OK to use a O(N^2) sorting algorithm

Getting the starter package

For many assignments this term, you'll receive some starter code, some testing tools, or miscellaneous other materials to help you get started. These will generally be delivered to you via downloadable tar files. As noted in this handy tutorial from Indiana University, you can extract the files and folders contained in a tar file by using the command:

tar xvf whatever.tar

To get started on this first assignment:

Automated testing

In the starting-c-package.tar file linked at the top of this page, you will find:

Note that for most assignments, I will only give you very simple tests as part of an assignment's starter package. The grader and I will certainly add some more sophisticated tests to explore the boundaries of a given assignment. You are, of course, free to use the testing infrastructure from the starter package to add your own tests. Getting used to automated testing and to writing detailed tests of your own will serve you well in the long-run.

Submitting your work

A little advice

Was it all over too soon?

Want an extra C challenge to fill your quiet hours? Try writing the word-counting program for fun. I will not grade this program, nor will you get any course credit for it. But the practice won't hurt.

Here's a more detailed description of this program.

wordcounter. This program's command-line syntax will be:

./wordcounter textfile

The program will read the words from the input file, count the number of times each word occurs in the file, and print to standard output a list of words and their counts, in the following format:

the,37 and,12 for,6 in,4 of,4 were,4 ...

That is, each line of output consists a word, then a comma, then the base-10 count of the number of occurrences of that word, with no spaces. The lines of output are sorted in reverse order of their counts, with ties broken by putting words in alphabetical order (see in/of/were above). You should assume that: