#include <stdlib.h> #include <stdio.h> #include \"string.h\" STRING string_init_
ID: 3673058 • Letter: #
Question
#include <stdlib.h>
#include <stdio.h>
#include "string.h"
STRING string_init_default_function(void)
{
String* pString = (String*)malloc(sizeof(String));
if(pString)
{
pString->capacity = 7;
pString->size = 0;
pString->char_arr = (char*) malloc(sizeof(char)*pString->capacity);
if(!pString->char_arr)
{
free(pString);
pString = NULL;
}
}
return pString;
}
STRING string_init_c_string(char* str)
{
int i = 0;
String* pString = (String*)malloc(sizeof(String));
if(pString)
{
while(str[i]!='')
{
i++;
}
pString->capacity = i;
pString->size = i-1;
pString->char_arr = (char*) malloc(sizeof(char)*pString->capacity);
if(!pString->char_arr)
{
free(pString);
pString = NULL;
}
for(i = 0; i < pString->capacity; i++)
{
pString->char_arr[i] = str[i];
}
}
return pString;
}
void string_destroy(STRING* pPString)
{
String* pString = (String*) *pPString;
free(pString->char_arr);
free(pString);
*pPString = NULL;
}
This is the String function. I need to build anew str_comp function.
Learning outcomes: - Functions - Struct - Pointers - Dynamic memory allocation - Starting building an opaque object
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 lower value in str1 than in str2 - >0: the first character that does not match has a higher value in str1 than in str2
Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
#include "string.h"
typedef struct String {
int capacity;
int size;
char *char_arr;
}String;
typedef String* STRING;
int str_comp(String* str1, String* str2) {
int i;
if(str1) {
if(str2) {
for(i = 0; i < str1->capacity; i++)
{
if(i < str2->capacity) {
if(str1->char_arr[i] < str2->char_arr[i])
return -1;
else if(str1->char_arr[i] > str2->char_arr[i])
return 1;
} else
//first string is longer and 2nd is subset of the first string
return 1;
}
//if both the lengths of string are equal and come out of above for loop successfully, then both string are equal
if(str1->capacity == str2->capacity)
return 0;
}
//if 2nd string is null, 1st string is greater
return 1;
}
}
STRING string_init_default_function(void)
{
String* pString = (String*)malloc(sizeof(String));
if(pString)
{
pString->capacity = 7;
pString->size = 0;
pString->char_arr = (char*) malloc(sizeof(char)*pString->capacity);
if(!pString->char_arr)
{
free(pString);
pString = NULL;
}
}
return pString;
}
STRING string_init_c_string(char* str)
{
int i = 0;
String* pString = (String*)malloc(sizeof(String));
if(pString)
{
while(str[i]!='')
{
i++;
}
pString->capacity = i;
pString->size = i-1;
pString->char_arr = (char*) malloc(sizeof(char)*pString->capacity);
if(!pString->char_arr)
{
free(pString);
pString = NULL;
}
for(i = 0; i < pString->capacity; i++)
{
pString->char_arr[i] = str[i];
}
}
return pString;
}
void string_destroy(STRING* pPString)
{
String* pString = (String*) *pPString;
free(pString->char_arr);
free(pString);
*pPString = NULL;
}
int main() {
STRING str1 = string_init_c_string("apple");
STRING str2 = string_init_c_string("ball");
int match = str_comp(str1, str2);
printf("%d",match);
str1 = string_init_c_string("cpple");
str2 = string_init_c_string("ball");
match = str_comp(str1, str2);
printf("%d",match);
str1 = string_init_c_string("cpp");
str2 = string_init_c_string("ball");
match = str_comp(str1, str2);
printf("%d",match);
str1 = string_init_c_string("apple");
str2 = string_init_c_string("app");
match = str_comp(str1, str2);
printf("%d",match);
str1 = string_init_c_string("apple");
str2 = string_init_c_string("apple");
match = str_comp(str1, str2);
printf("%d",match);
}
--output---
-11110
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.