Assignment #5: C Library Type

Implement a library type String, providing implementations of the following:
typedef struct String
{
    char *buffer;     /* Underlying null terminated string */
    int length;       /* The length of this String */
    int maxLength;    /* maximum length string that buffer can hold */
} String;

String *newString( const char *str );
void deleteString( String *str );

int st_length( const String * str );
const char * st_cstr( String *str );   // return char * equivalent
int st_equals( const String *str1, const String *str2 );
int st_compareTo( const String *str1, const String *str2 );

void st_copy( String *target, const String *source );
void st_appendChar( String *target, char ch )
void st_appendTo( String *target, const String *partToAdd );
String *st_plus( const String *str1, const String *str2 );

void print( const String *target );
int scanLine( String *target );     /* return false if immediate EOF */
String ** split( String *str );
An example of this functionality is:
String *s1 = newString( "hello" );
String *s2 = newString( "world" );
String *s3 = newString( "" );

int len1 = st_length( s1 );          /* len1 is 5 */
printf( "%s\n", st_cstr( s1 ) );     /* prints "hello" */

int eq1 = st_equals( s1, s2 );       /* eq1 is 0 */
int lc1 = st_compareTo( s1, s2 );    /* lc1 is less than 0 */

st_copy( s3, s1 );                   /* s3 will represent "hello" */
st_appendTo( s3, s2 );               /* s3 will represent "helloworld" */

String *s4 = st_plus( s1, s2 );      /* s4 will represent "helloworld" */

String *s5 = newString( "" );        /* If input is: ab cd ef */
scanLine( s5 );                      /* s5 will represent "ab cd ef" */

String **arr = split( s5 );          /* arr will be array of length 4 */
                                     /* arr[0] represents "ab" */
                                     /* arr[3] is NULL */
For scanLine, the return value is true if the read succeeds. Thus, if the line is empty, the result is an empty string of length zero and a return value of true. However, if the input has already been exhausted, then the result is an empty string AND the return value is false.

Provide a test program that illustrates all your functions. Please include in your tests these calls that should work:

st_copy( s, s );
st_copy( sm, lg );                   /* copy large into small */
st_appendTo( s, s );
Also, be sure that N consecutive calls to st_appendChar starting from an empty string takes only O(N) time. The easiest way to do this to double the resulting string length as the new maximum string length during any buffer expansion. This will allow you to use st_appendChar to easily implement scanLine. You should have three files: a MyString.h file that describes the functionality of the String type, a MyString.c file that implements this functionality, and assign5.c that provides the test program. I will have a separate test program, so be sure to have a test program that hits as many cases as possible.