Library of functions for manipulating arrays of strings in C

You may work alone or in groups of two on this assignment. If you work in a group, please submit a single group solution signed by both group members.


To Do

In your systems directory, create another directory called stringarray. In this new directory create three files: two C source files named driver.c and stringarray.c, and a header file named stringarray.h. The driver source file is the one containing the main function. The other two files contain implementations (C source file) and declarations (header file) for the functions listed below:
  1. Function that initializes all entries in an array to NULL:
          void InitArray(char *array[], int n);  
          /* Input:  @param array is an array of uninitialized pointers 
           *         @param n is the number of entries in the array 
           * Action: Initialize all entries in the array to NULL */ 
    Note: The function InitArray assumes that no strings exist in the array at the time it is called.

  2. Function that inserts a new string in the array:
          void Insert(char *array[], char * s, int pos); 
          /* Input:  @param array is an array of strings
           *         @param s is a string to be inserted in the array 
           *         @param pos is a valid array index
           * Action: Insert s in position pos in the array */
    Note: You will need to use malloc to dynamically allocate memory for the string to be inserted in the array, and strcpy to copy the string into the newly allocated memory.

  3. Function that removes a string from the array:
          void Remove(char *array[], int pos); 
          /* Input:  @param array is an array of strings
           *         @param pos is a valid array index 
           * Action: Remove the string from position pos in the array (if any) */ 
    Note: If the entry value is NULL, Remove does not do anything. Otherwise, Remove invokes free to deallocate the memory space allocated for the string with malloc, so that it does not create any memory leaks. Reset the array entry to NULL to mark that no strings are attached to it.

  4. Function that clears the entire array (deallocates all strings and sets all entries to NULL):
          void RemoveAll(char *array[], int n); 
          /* Input:  @param array is an array of strings
           *         @param n is number of entries in the array
           * Action: Remove all existing strings from the array */
    Note: RemoveAll may invoke Remove to do the job.

  5. Function that prints the string stored at a particular array entry:
          void Print(char *array[], int pos); 
          /* Input:  @param array is an array of strings
           *         @param pos is a valid array index
           * Action: Print out the string at position pos (if any) in the array */
    Note: Print must check first if the array entry is NULL (otherwise the printf function will crash if invoked with a NULL pointer as an argument).

  6. Function that prints all strings in the array, in order:
          void PrintAll(char *array[], int n); 
          /* Input:  @param array is an array of strings
           *         @param n is the number of entries in the array
           * Action: Print out all strings in the array (i.e., all non-NULL array entries) */
Here is some sample code that uses these functions, along with memory snapshots after each function invocation.

Code Memory snapshot of myarray
   #define MAX 8 
   char * myarray[MAX];
   InitArray(myarray, MAX);
   Insert(myarray, "one", 0);
   Insert(myarray, "two", 1);
   Insert(myarray, "three",2);
   Remove(myarray, 1);
   PrintAll(myarray, MAX);
 Output is:

A sample driver is provided. Feel free to modify it, but make sure that it thoroughly tests the correctness of each function.

To compile your code, use must supply all source (not header) files to the gcc complier:

      gcc stringarray.c driver.c -o driver 
The result is an executable called driver.

Alternately, you may use this Makefile and simply type in make at the shell prompt to compile your code. This saves you from typing in a long command line every time you need to compile your code. Makefiles are special format files that help build and manage projects composed of multiple modules. The key point to remember is that you need to insert a TAB character (not spaces) before each command in the Makefile. If interested, you may read more about using make and writing Makefiles.

Important Notes

Submission Instructions

In your stringarray directory create a text file named readme (not readme.txt, or README, or Readme, etc.) that contains:
  1. Your name(s) and assignment number.
  2. A description of whatever help (if any) you received from others outside of class.
  3. An indication of how much time you spent doing the assignment outside of class.
  4. Your assessment of the assignment: Did it help you to learn? What did it help you to learn? Do you have any suggestions for improvement? Etc.
  5. Any information that will help us to grade your work in the most favorable light. In particular you should describe all known bugs.
Descriptions of your code should not be in the readme file. Instead they should be integrated into your code as comments. Your readme file should be a plain text file. Do not create your readme file using Microsoft Word or any other word processor.

Turn in a printout copy of the readme file, and each of the three files driver.c, stringarray.c and stringarray.h (one printout per group). You will need to copy and paste your code into a text or a Word document in Windows, since you cannot print directly from Unix.

Leave the source code for all exercises in your directory systems/stringarray. Do not make any changes to these files after the due date for this assignment. If you wish to continue working on these exercises after the due date, make a copy of your directory stringarray using the following Unix command:

    cp -r ~/systems/stringarray ~/systems/stringarray-copy
A new directory called stringarray-copy will be created in your systems directory. You may now make any changes you want to the files in your stringarray-copy directory, at any time.


No credit will be given for code that does not compile.
100     Total points possible
20 Program compiles and runs without crashing
10 Driver includes all test cases (for both null and non-null entries)
60 Each function performs its specified action
10 Your readme file is present

Have fun!