Building the string object: phase1 Learning outcomes: - Functions - Struct - Poi
ID: 3673056 • Letter: B
Question
Building the string object: phase1 Learning outcomes: - Functions - Struct - Pointers - Dynamic memory allocation - Starting building an opaque object Lab details: In this lab, you will learn how to create an opaque object by building the string object step by step. A string object basically holds an array of chars representing a string. It also holds information associated with this string including its size and its capacity. YOU CANNOT USE ANY FUNCTIONS FROM THE STRING LIBRARY. In this phase, you will create: - A string struct - string_init_default function - string_init_c_string - string_destroy - string_compare 1. string struct: The string struct has three members: - A character char_arr: to hold the characters representing the string. The size of the array is not determined yet. Therefore, it is represented as a char*. - An int capacity: the maximum number of characters the array could hold - An int size: the current number of characters in the array 2. string_init_default function: This function is responsible for initializing a string object. It takes the capacity of the array inside the string as input and return a struct string* as an output. In more details, the function does the following: - Allocates a memory for a struct string* pString (making sure that the memory was successfully allocated) - Sets pString->capacity to be equal to 7. - Dynamically allocates pString->char_arr to hold pString->capacity number of characters (makes sure that the memory was successfully allocated) - Sets the pString->size to zero - Finally, returns pString 3. string_init_c_string: This function takes a regular c_string as input and creates a string object. The string object is initialed with the c_string. In more details, the function does the following: - Takes a regular c_string as input COMP 1020 lab2 Hatem - Allocates a memory for struct string* pString (making sure that the memory was successfully allocated) - Determines the size of the c_string and sets pString->capacity to be 1 bigger than the number of characters in the c_string. (So for the c_string “the” it would make a capacity of 4.) - Dynamically allocates pString->char_arr using the calculated capacity (makes sure that the memory was successfully allocated) - Copies the c_string into pString->char_arr - Sets pString->size to be equal the number of characters inside pString->char_arr - Finally, returns pString 4. string_destroy: This function is responsible for clearing and deallocating the memory used by the string object. In more details, the function does the following: - Takes a struct string ** pPString as input - It deallocates (frees) the memory for the (*pPString)->char_arr - It deallocates (frees) the memory for the (*pPString) - Finally, it sets (*pPstring) to NULL - The function does not return anything 5. string_compare: The function takes two string object and compares between them. The function behaves similar to the strcmp function. Basically, the function needs to return the following: - 0: both strings are equal - 0: the first character that does not match has a higher value in str1 than in str2.Please help me out with this. I need to build a new str_copy function
Explanation / Answer
OUTPUT
str1: Sample string
str2: Sample string
str3: copy successful
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.