Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help to write a test for my function my_string_assignment. The requirement for the test is that Create an array of MY_STRING handles with 100

Need help to write a test for my function "my_string_assignment". The requirement for the test is that "Create an array of MY_STRING handles with 100 elements. Initialize each element of the array to NULL. Use your init_c_string function to initialize the first element of the array to the string COPY ME!. Write a for loop that uses your assignment function to copy the first string into every other element of the array. Destroy every element of the array with a for loop calling destroy on each element but use string_insertion to print each element to the screen just before deleting it."

Following are the code with associated functions:

#define MY_STRING void*
struct my_string {
int capacity;
int size;
char* data;
};
typedef struct my_string My_string;

MY_STRING my_string_init_c_string(const char* c_string) {
int i;
My_string* pString;
pString = malloc(sizeof(My_string));
if (pString == NULL) {
free(pString); pString = NULL;
return pString;
}
for (i = 0; c_string[i] != '\0'; i++);
pString->capacity = (i + 1);
pString->size = i;
pString->data = (char*)malloc(sizeof(char)*pString->capacity);
if (pString->data == NULL) {
free(pString->data); pString->data = NULL;
return pString->data;
}
for (i = 0; i < pString->capacity; i++) {
pString->data[i] = c_string[i];
}
return pString;

}

void my_string_destroy(Item_ptr* pItem_ptr) {
My_string* pString = (My_string*)*pItem_ptr;
free(pString->data); pString->data = NULL;
free(pString); pString = NULL;
*pItem_ptr = NULL;
return;

}

Status my_string_insertion(MY_STRING hMy_string, FILE* fp) {
My_string* pMy_string = (My_string*)hMy_string;
int i = 0;
while (i < pMy_string->size) {
fprintf(fp, "%c", pMy_string->data[i]);
i++;
}
return SUCCESS;
}
void my_string_assignment(Item_ptr* pLeft, Item_ptr Right)
{
My_string* pMy_string_left = (My_string*)*pLeft;
My_string* pMy_string_right = (My_string*)Right;
int i;
const char* temp;
temp = my_string_c_str((MY_STRING)Right);
if (pMy_string_left == NULL) {
pMy_string_left = my_string_init_c_string(temp);
}
else {
pMy_string_left->size = 0;
for (i = 0; i < pMy_string_right->size + 1; i++) {
my_string_push_back(pMy_string_left, pMy_string_right->data[i]);
}
}
*pLeft = pMy_string_left;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

CL I P COL Astro- L(1-cas0) Lsing *A=2 L sin(0/2)

Answered: 1 week ago