CSC 173, Fall 2006

Assignment 2: C

This assignment consists of two programming questions. Please use the gnu (Free Software Foundation) C compiler, gcc. To help with debugging, use the gnu debugger, gdb. valgrind DUE DATE: 11:59 pm, Friday October 6.


Watch the discussion group for possible updates.

Programming questions

  1. Write a C program that will echo standard input to standard output, one line at a time, after potentially encoding or decoding the input as specified below. Your code must include the following lines:
    #include <stdio.h>
    #include <stdlib.h>
    void get_line(char *s,int offset, char mode) {
            /* your code here */
    void put_line(char *s) {
            /* your code here */
    int main(int argc,char **argv) {
        char s[300], mode;
        int offset;
        if ((argc < 2) || (argc > 3)) {
            printf("Usage: %s {e|d|p} n\n   where n is an integer from 0 to 255.\n",argv[0]);
            return 1;
        else {
            if ((mode == 'e') || (mode == 'd')) {
                if (argc < 3) {
                    printf("Please supply an offset.\n");
                    return 1;
            if ((offset < 0) || (offset > 255)) {
      	  printf("Usage: %s {e|d|p} n\n   where n is an integer from 0 to 255.\n",argv[0]);
      	  return 1;
        while(1) {
            if(!s[0]) break;
    You should modify only the places where /* your code here */ appears. Your code for get_line and put_line should duplicate the functionality of the standard gets and puts library routines, except that (1) you need not return a useful value, (2) get_line should not strip the newline characters off the end of its input lines, and put_line should not add a newline to its output lines (This behavior with respect to newlines is the same as displayed by the file-specific fgets and fputs routines.), and (3) you will be adding encoding/decoding functionality. get_line should do the following:

    Within get_line and put_line you should use the getchar and putchar library routines. You are not permitted (for this one assignment) to use gets, puts, scanf, or printf in order to read from/to stdin/stdout. You may, however, use sprintf and atoi for internal manipulation of your characters.

    Your code must reside in a file named codec.c in a directory named codec, which must be an immediate subdirectory of the directory in which you run the turnin script.

    Finally, in your README.txt or README.pdf file, you must answer the following questions: What may happen if you feed your codec program input containing a line longer than 100 characters? What might you do about this problem?

  2. Building upon your solution to the previous question, write a C program that will print its input lines in reverse order, either in plain text or after encryption or decryption. That is, if the input contains N lines, the program will print line N, followed by line N-1, etc., down through line 1. The characters within any individual line will be printed in the original order. (In other words, you should duplicate the behavior of the standard utility tail when run with the -r command-line switch (except for the encryption/decryption).)

    You should store your strings internally in a linked list. You may place a (large) fixed limit on the length of an input line, but you should try not to use that much space for every line: once read into memory, a line should be copied to dynamically allocated space of the appropriate size. Your code should handle an arbitrary number of input lines. You may use any of the functions in the <string.h> library package, with the exception of strdup; type "man 3 string" to learn more.

    Prior to terminating, your program must deallocate (free) any dynamically allocated (malloced) space. (Your program will appear to run correctly even if you don't, but your code would then "leak storage" if used as part of a larger system. The TAs will be checking for proper memory cleanup.

    Your code must reside in a directory named reverselines, which must be an immediate subdirectory of the directory in which you run the turnin script.

What to hand in

Documentation for the programming questions must appear in your README.txt or README.pdf file. This file must be located in the directory in which you run the turnin script. You should also provide a Makefile(this tutorial about Make might be helpful) in order to facilitate the compilation of your code. The only other things in this directory should be the two subdirectories codec and reverselines.

Back to the grading page

Back to the course home page

Last Change: 25 September 2006