Library of functions for manipulating arrays of strings in C

This is a group assignment. You may work in groups of two to three on this assignment.

In the previous assignment you learned about command arguments that can be passed to the main function as an array of strings. For example, if you write a program

    int main(int argc, char * argv[])
    { ...}
and invoke the executable using the command line
    ./a.out one two three
then the value 4 will be passed to argc in the main function, and the string array argv will be allocate memory space as in the diagram below.


In this assignment you will create a library of functions that manipulate arrays of strings similar to the argv array depicted above. You will be using this library in future assignments, so make sure that your code is correct and clean.

You may want to review C strings and dynamic memory allocation before starting on this assignment.

In your csc1600 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.

      void InitArray(char *array[], int n);  
      /* Input:  array is an array of uninitialized pointers 
                 n is the number of entries in the array 
         Action: Initialize all entries in the array to NULL */ 
Note: The function Init assumes that no strings exist in the array at the time it is called.

      void Insert(char *array[], char * s, int pos);
      /* Input:  array is an array of strings
                 s is a string to be inserted in the array 
                 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.

      void Remove(char *array[], int pos);
      /* Input:  array is an array of strings
                 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 it (no strings attached to it).

      void RemovaAll(char *array[], int n);
      /* Input:  array is an array of strings
                 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.

      void Print(char *array[], int pos);
      /* Input:  array is an array of strings
                 pos is a valid array index
         Action: Print out the string from 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 it is invoked with a NULL pointer).

      void PrintAll(char *array[], int n);
      /* Input:  array is an array of strings
                 n is the number of entries in the array
         Action: Print out all 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];
   Init(myarray, MAX);
   Insert(myarray, "one", 0);
   Insert(myarray, "two", 1);
   Insert(myarray, "three",2);
   Remove(myarray, 1);
   PrintAll(myarray, MAX);
 Output is:

Your driver should include code that thoroughly tests the correctness of each function.

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 (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 csc1600/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 ~/csc1600/stringarray ~/csc1600/stringarray-copy
A new directory called stringarray-copy will be created in your csc1600 directory. You may now make any changes you want to the files in your stringarray-copy directory, at any time.


40     Total points possible
10 Program compiles successfully and runs
15 Each function performs its specified action
10 Code does not break if functions are passed NULL parameters
 5 Your readme file is present

Have fun!