Question
In C, use the following requirements to modify p1.h and p1.c . Modify p1.h and p1.c in the YOUR CODE HERE sections p1.h : #include
In C, use the following requirements to modify p1.h and p1.c.
Modify p1.h and p1.c in the "YOUR CODE HERE" sections
p1.h :
#include
struct strstr { /* YOUR CODE HERE */ }; typedef struct strstr *str_t;
/* create a new string variable containing the given char sequence */ /* precondition: initializer is non-null */ /* precondition: initializer points to a null-terminated string */ str_t create(char *initializer); /* return the length of the string */ /* precondition: s has been initialized (created) */ int length(str_t s); /* return 1 if s1 and s2 represent the same string, 0 otherwise */ /* precondition: both arguments are non-NULL */ int equals(str_t s1, str_t s2); /* make the contents of "to" be equal to the contents of "from". * Precondition: both are valid (initialized) strings. * Note: this is what is called a "deep copy" - subsequent changes * to "from" do not affect "to". */ void copy(str_t to, str_t from); /* modify the contents of the first argument by replacing every * character in it that also occurs in the second argument with a * space character (integer value \verb.0x20.). In other words, * this has the same semantics as the corresponding function in Project 0. */ void censor(str_t orig, str_t bad); /* return a pointer to a character array that contains * the same sequence of characters as s, but with the end marked by a * null (0) byte, according to the C convention. * Subsequent changes to s MAY change the returned C string, including modifying * the end marker. This is intended for ephemeral, one-time use, e.g., * passing to printf(). */ char *to_chars(str_t s); /* Extend string s1 by appending the string s2 to it. s2 is unmodified. */ void append(str_t s1, str_t s2); /* Return a new string of len characters equal to the len characters of s * beginning at position start. The original string s is unchanged. * If start + len exceeds the actual length of the string, the * returned string consists of the characters from index start to the end of s. * Precondition: both start and len are at least 0. */ str_t substring(str_t s, int start, int len); #endif
----------------------------------------------------------------------------------------------------------
p1.c :
#include "p1.h"
/* create a new string variable containing the given char sequence */ /* precondition: initializer is non-null */ /* precondition: initializer points to a null-terminated string */ str_t create(char *initializer) { /* YOUR CODE HERE */ }
/* return the length of the string */ /* precondition: s has been initialized (created) */ int length(str_t s) { /* YOUR CODE HERE */ }
/* return 1 if s1 and s2 represent the same string, 0 otherwise */ /* precondition: both arguments are non-NULL */ int equals(str_t s1, str_t s2) { /* YOUR CODE HERE */ }
/* make the contents of "to" be the contents of "from" */ /* precondition: both are valid (initialized) strings */ void copy(str_t to, str_t from) { /* YOUR CODE HERE */ }
/* modify the contents of the first argument by replacing every * character in it that also occurs in the second argument with a * space character (integer value \verb.0x20.). In other words, * this has the same semantics as the corresponding function in Project 0. */ void censor(str_t orig, str_t bad) { /* YOUR CODE HERE */ }
/* return a pointer to a character array that contains * the same sequence of characters as s, but with the end marked by a * null (0) byte, according to the C convention. * Subsequent changes to s may change the returned C string, including modifying * the end marker. This is intended for ephemeral, one-time use, e.g., * passing to printf(). */ char *to_chars(str_t s) { /* YOUR CODE HERE */ }
/* Extend string s1 by appending the string s2 to it. s2 is unmodified. */ void append(str_t s1, str_t s2) { /* YOUR CODE HERE */ }
/* Return a new string of len characters equal to the len characters of s * beginning at position start. The original string s is unchanged. * If start + len exceeds the actual length of the string, the * returned string consists of the characters from index start to the end of s. * Precondition: both start and len are at least 0. */ str_t substring(str_t s, int start, int len) { /* YOUR CODE HERE */ }
-------------------------------------------------------------------------------------------------
You can test using p1simple.c :
#include
/* NOTE: This code is a quick-and-dirty test driver. * It is NOT an example of good code hygiene, because of the many naked * constants and cryptic outputs. But it should be helpful in debugging. */ int main() { int retval, i; str_t shortstr = create("a short string"); str_t alpha = create("abcdefghijkl"); str_t beta = create("b"); str_t empty = create(""); str_t bad = create("bad"); str_t s = create("nothing"); str_t last; retval = length(shortstr); if (retval != 14) printf("Fail: wrong length on shortstr: %d. ",retval); else printf("1"); retval = length(alpha); if (retval != 12) printf("Fail: wrong length on alpha: %d ",retval); else printf("..2"); retval = length(beta); if (retval != 1) printf("Fail: wrong length on beta: %d ",retval); else printf("..3"); retval = length(empty); if (retval != 0) printf("Fail: wrong length on s: %d ",retval); else printf("..4"); copy(s,shortstr); if (!equals(s,shortstr)) printf("Fail: shortstr != s after copy. "); else printf("..5");
/* censor() and to_chars() */ censor(alpha,bad); printf(" censor output (should be [ c efghijkl]): [%s] ", to_chars(alpha));
/* append and substring */ copy(s,empty); for (i=0; i
last = substring(s,24,12); if (!equals(last,alpha)) printf("Fail: incorrect substring of s: expected %s, got %s. ", to_chars(alpha),to_chars(last)); else printf("...7. "); return 0; }
A header file p1.h containing the above type declarations is provided on the Canvas page. A template file p1_template.c is also provided. You will modify the latter file (by replacing the "YOUR CODE HERE" comments with C code). Your code in p1.c must satisfy the following constraints: 1. Self-contained. Your implementations must not call any function other than those defined in your p1.c file (plus malloc, calloc, and/or realloc). If you need auxiliary functions you must define them in p1.c. The file you turn in (see below) must not have any additional \#include statements. (The \#include you need for the memory allocation functions will be in p1.h.) 2. Your definitions of the functions must exactly match the declarations in p1.h. (This is true of the given p1_template.c.) 3. The file contains your implementations of the above functions and nothing else. In particular, it must not contain a main function. 4. Your code must compile ( gcCWallc pl.c) without generating any errors or warningsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started