Question: Please answer both 1 and 2. The code for Project 1 as referred to in 1 and 2 is provided below. Project 1 Code: #include

Please answer both 1 and 2. The code for "Project 1" as referred to in 1 and 2 is provided below.

Please answer both 1 and 2. The code for "Project 1" as

referred to in 1 and 2 is provided below. Project 1 Code:

Project 1 Code:

#include "p1.h" #include /* invariants: * strt_capacity >= strt_length * strt_length is the number of valid chars in strt_content[] */ #define DFLTCAP 128 #define EXTRA 16 /* 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) { /* determine the length of the C-style argument */ int cap = DFLTCAP; int l = 0; int need = EXTRA; while (initializer[l]) l++; need += l; while (need > cap) cap strt_length = l; newone->strt_capacity = cap; newone->strt_content = malloc(cap); char *cp = newone->strt_content; for (int i=0; istrt_length; } /* 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) { if (s1->strt_length != s2->strt_length) return 0; /* s1 and s2 are the same length */ for (int i=0; istrt_length; i++) if (s1->strt_content[i] != s2->strt_content[i]) return 0; return 1; } /* make the contents of "to" be the contents of "from" */ /* precondition: both are valid (initialized) strings */ void copy(str_t to, str_t from) { int i; /* do we have enough capacity? */ int need = from->strt_length + EXTRA; while (need > to->strt_capacity) // increase by standard increments to->strt_capacity += DFLTCAP;

/* tp->strt_capacity >= from->strt_length + EXTRA */ to->strt_content = realloc(to->strt_content,to->strt_capacity); to->strt_length = from->strt_length; for (i=0; istrt_length; i++) to->strt_content[i] = from->strt_content[i]; } /* 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 0x20). In other words, * this has the same semantics as the corresponding function in Project 0. */ void censor(str_t orig, str_t bad) { int i, j; for (i=0; i strt_length; i++) for (j=0; j strt_length; j++) if (orig->strt_content[i] == bad->strt_content[j]) orig->strt_content[i] = ' '; } /* 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) { /* We just add a null-terminator and return a pointer to current content * pointer. */ if (s->strt_capacity == s->strt_length) { // no room for null /* XXX paranoia - this should not happen */ s->strt_content = realloc(s->strt_content,s->strt_length + EXTRA); } /* s->strt_content has at least one byte beyond the current string */ s->strt_content[s->strt_length] = 0; return s->strt_content; } /* Extend string s1 by appending the string s2 to it. s2 is unmodified. */ void append(str_t s1, str_t s2) { int i,j; /* first figure out how much space we need */ int size = s1->strt_capacity; int needed = s1->strt_length + s2->strt_length + EXTRA; if (size = needed */ s1->strt_content = realloc(s1->strt_content,size); s1->strt_capacity = size; } /* where to copy */ size = s1->strt_length + s2->strt_length; /* size is now the length of s1 after appending */ for (i=s1->strt_length, j=0; i

s1->strt_content[i] = s2->strt_content[j]; s1->strt_length = size; /* invariants re-established */ } /* 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) { int i,j; /* We are to return a new str_t instance no matter what. */ str_t newstr = malloc(sizeof(struct strstr)); /* If the start position is beyond the end, just return an empty string */ if (start >= s->strt_length) { newstr->strt_length = 0; newstr->strt_capacity = DFLTCAP; newstr->strt_content = malloc(DFLTCAP); return newstr; } /* start strt_length */ /* the substring mustn't extend beyond the end of s */ if (len > s->strt_length - start) len = s->strt_length - start; /* start + len strt_length */ /* now find the smallest chunksize that will hold the substring */ int need = len + EXTRA; newstr->strt_capacity = DFLTCAP; while (newstr->strt_capacity strt_capacity += DFLTCAP; newstr->strt_length = len; newstr->strt_content = malloc(newstr->strt_capacity); for (i=0, j=start; i strt_content[i] = s->strt_content[j]; return newstr; }

1.) As noted in lecture, there are often multiple ways to code up the same algorithm in C. This problem refers to the solution for Project 1 given on the assignment page in Canvas. a. Write a 2-line while loop to replace the 2-line for loop in the function create(). Do not use any integer variables in your loop. b. Rewrite the body of the for-loop in the function equals () so that it does not contain any square brackets ([]). A single assignment statement will suffice. c. Do the same thing for the body of the nested for-loops in censor (). 2.) Suppose in implementing Project 1 you wanted to keep track of how many str_ts were create()-ed. How might you do it in such a way that you were guaranteed that only your code (i.e., your implementation of create () could access the count

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!