Question
Answer in C language, add comments to code, three files included main.c, Stringstr.c, Stringstr.h each file indicated in bold, NOTE: Must be a deep copy
Answer in C language, add comments to code, three files included main.c, Stringstr.c, Stringstr.h each file indicated in bold, NOTE: Must be a deep copy
Provide screenshot of implemented code and output, task description located in main
//Stringstr.h file contents
#ifndef STRING_STR #define STRING_STR
typedef struct StringString { // This holds the length of the string int length;
// pointer to the raw data // data is a pointer to an array of characters (c-string) // note that the size of the array data points to is 1 longer // than 'length', because it will hold the terminating char char *data; } SSTRING;
// ALREADY IMPLEMENTED Create a StringString based on a s_str SSTRING *SStr_Create(char *s_str);
// ALREADY IMPLEMENTED Clean up a StringString void SStr_Delete(SSTRING *str);
// ALREADY IMPLEMENTED A Print function for StringString void SStr_Print(SSTRING *str);
// Append str_to_append to str void SStr_Append(SSTRING *str, SSTRING *str_to_append);
#endif
//Stringstr.c contents
#include "Stringstr.h" #include
SSTRING* SStr_Create(char *str_to_copy) { SSTRING *new_s = (SSTRING *)malloc(sizeof(SSTRING));
new_s->length = strlen(str_to_copy);
// Allocate data array dynamically. // Add one to data length for string terminator new_s->data = (char *)malloc(new_s->length+1);
// Copy data over from str_to_copy for (int i = 0; i < new_s->length; i++) { new_s->data[i] = str_to_copy[i]; } new_s->data[new_s->length] = '\0'; return new_s; }
void SStr_Delete(SSTRING *str) { free(str->data); free(str); }
void SStr_Print(SSTRING *str){ printf("{ len: %d, data: %s } ", str->length, str->data); }
//main.c contents
#include "Stringstr.h" #include
/* Add an append function for the StringString object . Functions already implemented : Create, Delete, printing
Add the implementation for a function void SStr_Append(SSTRING* str, SSTRING* str_to_append)
The first argument is the string that will be appended to. The second argument is the string to append.
For example, // str1 contains "Chocolate" // str2 contains "Cookie" SStr_Append(str1, str2); // str1 now contains "Chocolatecookie"
NOTE: deep copy of the str_to_append */
int main(void) { printf("sstr1, Create "); char *s_str1 = "Hello there"; SSTRING *s_str1 = SStr_Create(cstr1); SStr_Print(s_str1);
printf("sstr2, Create "); SSTRING *s_str2 = SStr_Create(" goodbye now"); SStr_Print(s_str2);
/* Append example, uncomment when SStr_Append() is implemented printf("sstr1 appends sstr2 "); SStr_Append(s_str1, s_str2);
// sstr1 should now be 'Hello there goodbye now' printf("Result sstr1 : "); SStr_Print(s_str1);
// sstr2 should be unchanged printf("Result sstr2 : "); SStr_Print(s_str2); */
printf("sstr1, Delete "); SStr_Delete(s_str1); printf("sstr2, Delete "); SStr_Delete(s_str2); return 0; }
Step 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