Question
I need help with creating a main file that tests the functions Here is a start to the main file #include #include #include my_string.h int
I need help with creating a main file that tests the functions
Here is a start to the main file
#include #include #include "my_string.h"
int main(int argc, char* argv[]) {
return 0; }
my_string.c
#include "my_string.h"
#include
#include
struct my_string{
int size;
int capacity;
char* data;
};
typedef struct my_string My_String;
MY_STRING my_string_int_default(void)
{
My_String* pMy_String;
pMy_String = (My_String*)malloc(sizeof(My_String));
if(pMy_String != NULL)
{
pMy_String->size = 0;
pMy_String->capacity = 7;
pMy_String->data = (char*)(malloc(sizeof(int)*pMy_String->capacity));
if(pMy_String->data == NULL)
{
free(pMy_String);
pMy_String = NULL;
}
}
return pMy_String;
}
void my_string_destroy(MY_STRING* phMy_String)
{
My_String* pMy_String = (My_String*)*phMy_String;
free(pMy_String->data);
free(pMy_String);
*phMy_String = NULL;
}
MY_STRING my_string_init_c_string(const char*c_string)
{
int len = 0;
My_String* pMy_String;
pMy_String = (My_String*)malloc(sizeof(My_String));
while(c_string[len] != '\0')
len++;
if(pMy_String != NULL)
{
pMy_String->size = len;
pMy_String->capacity = len+1;
pMy_String->data = (char*)(malloc(sizeof(int)*pMy_String->capacity));
if(pMy_String->data==NULL)
{
free(pMy_String);
pMy_String = NULL;
}
for(int i=0; i
pMy_String->data[i] = c_string[i];
}
return pMy_String;
}
int my_string_get_capacity(MY_STRING hMy_string)
{
My_String* pMy_String = (My_String*)hMy_string;
return pMy_String -> capacity;
}
int my_string_get_size(MY_STRING hMy_string)
{
My_String* pMy_String = (My_String*)hMy_string;
return pMy_String -> size;
}
int my_string_compare(MY_STRING hLeft_string, MY_STRING hRight_string)
{
My_String* str1 = (My_String*) hLeft_string;
My_String* str2 = (My_String*) hRight_string;
for(int i =0; isize && isize; i++)
if(str1->data[i] != str2->data[i])
return str1->data[i] - str2->data[i];
return str1->size - str2->size;
}
Status my_string_extraction(MY_STRING hMy_string, FILE* fp)
{
My_String* string = (My_String*)hMy_string;
char c=fgetc(fp); int count = 0, i;
char* temp;
if(c == EOF)
return FAILURE;
while(c == ' ')
{
c = fgetc(fp);
}
while(c != EOF && c != ' ' && c != ' '){
string->size = count;
if (string->size >= string->capacity)
{
temp = (char*)malloc(sizeof(char) * 2 * string->capacity);
if (temp == NULL)
{
return FAILURE;
}
for (i = 0; i size; i++)
{
temp[i] = string->data[i];
}
free(string->data);
string->data = temp;
string->capacity = string->capacity * 2;
}
string->data[count] = c;
count++;
string->size = count;
c=fgetc(fp);
}
//printf("%d, %s ", string->size, string->data);
// string-> data[count] = c;
// string->size = count;
if( c == ' ')
{
ungetc(c,fp);
return SUCCESS;
}
if(count == '0')
return FAILURE;
return SUCCESS;
}
Status my_string_insertion(MY_STRING hMy_string, FILE* fp)
{
int i = 0;
My_String* string = (My_String*)hMy_string;
Status result = SUCCESS;
if(fp == NULL)
return FAILURE;
while(i
{
if(fputc(string->data[i], fp))
result = SUCCESS;
else
result = FAILURE;
i++;
}
return result;
}
Status my_string_push_back(MY_STRING hMy_string, char item)
{
My_String* pMy_string = (My_String*)hMy_string;
//if there enouth space
//if not- create space (resize)
int* temp;
int i;
if (pMy_string->size >= pMy_string->capacity)
{
temp = (int*)malloc(sizeof(int) * 2 * pMy_string->capacity);
if (temp == NULL)
{
return FAILURE;
}
for (i = 0; i size; i++)
{
temp[i] = pMy_string->data[i];
}
free(pMy_string->data);
pMy_string->data = temp;
pMy_string->capacity *= 2;
}
//we have space
pMy_string->data[pMy_string->size] = item;
pMy_string->size++;
return SUCCESS;
}
Status my_string_pop_back(MY_STRING hMy_string)
{
My_String* pMy_string = (My_String*)hMy_string;
if (pMy_string->size
{
return FAILURE;
}
pMy_string->size--;
return SUCCESS;
}
char* my_string_at(MY_STRING hMy_string, int index)
{
My_String* pMy_string = (My_String*)hMy_string;
if (index = pMy_string->size)
{
return NULL;
}
return &pMy_string->data[index];
}
char* my_string_c_str(MY_STRING hMy_string)
{
My_String* pMy_string = (My_String*)hMy_string;
char* temp;
int i;
if (pMy_string->data[pMy_string->size] != '\0')
{
temp = (char*)malloc(sizeof(char) * 2 * pMy_string->capacity);
if (temp == NULL)
{
return NULL;
}
for (i = 0; i size; i++)
{
temp[i] = pMy_string->data[i];
}
temp[i] = '\0';
free(pMy_string->data);
pMy_string->data = temp;
pMy_string->capacity = pMy_string->size + 1;
}
return pMy_string->data;
}
Status my_string_concat(MY_STRING hResult, MY_STRING hAppend)
{
My_String* pResult = (My_String*)hResult;
My_String* pAppend = (My_String*)hAppend;
char* temp;
int i, size, capacity;
if (pAppend->size == 0)
{
return FAILURE;
}
else if (pResult->size == 0)
{
for (i = 0; i size; i++)
{
pResult->data[i] = pAppend->data[i];
}
pResult->size = pAppend->size;
pResult->capacity = pResult->size + 1;
}
else
{
size = pResult->size + pAppend->size;
capacity = size + 1;
temp = (char*)malloc(sizeof(char) * capacity);
if (temp == NULL)
{
return FAILURE;
}
for (i = 0; i size; i++)
{
temp[i] = pResult->data[i];
}
for (i = 0; i size; i++)
{
temp[pResult->size + i] = pAppend->data[i];
}
temp[pResult->size + i] = '\0';
free(pResult->data);
pResult->data = temp;
pResult->capacity = capacity;
pResult->size = size;
}
return SUCCESS;
}
Boolean my_string_empty(MY_STRING hMy_string)
{
My_String* pMy_string = (My_String*)hMy_string;
if (pMy_string->size == 0)
{
return TRUE;
}
return FALSE;
}
my_string.h
#include #include "status.h" typedef void* MY_STRING;
MY_STRING my_string_int_default(void);
void my_string_destroy(MY_STRING* phMy_String);
MY_STRING my_string_init_c_string(const char*c_string);
int my_string_get_capacity(MY_STRING hMy_string);
int my_string_get_size(MY_STRING hMY_string);
int my_string_compare(MY_STRING hLeft_string, MY_STRING hRight_string);
Status my_string_extraction(MY_STRING hMy_string, FILE* fp);
Status my_string_insertion(MY_STRING hMy_string, FILE* fp);
Status my_string_push_back(MY_STRING hMy_string, char item);
Status my_string_pop_back(MY_STRING hMy_string);
char* my_string_at(MY_STRING hMy_string, int index);
char* my_string_c_str(MY_STRING hMy_string);
Status my_string_concat(MY_STRING hResult, MY_STRING hAppend);
Boolean my_string_empty(MY_STRING hMy_string);
status.h
enum status { FAILURE, SUCCESS }; typedef enum status Status;
Makefile
CC = gcc
CFLAGS = -Wall --std=c99 -g
OBJECTS = main.o my_string.o
string_driver: $(OBJECTS)
$(CC) $(CFLAGS) -o string_driver $(OBJECTS)
main.o: main.c
$(CC) $(CFLAGS) -c main.c -o main.o
my_string.o: my_string.c
$(CC) $(CFLAGS) -c my_string.c -o my_string.o
clean:
rm string_driver $(OBJECTS)
//Precondition: hMy_string is the handle to a valid My string object. //Postcondition: If successful, places the character item at the end of the // string and returns SUCCESS. If the string does not have enough room and /I cannot resize to accomodate the new character then the operation fails // and FAILURE is returned. The resize operation will attempt to amortize // the cost of a resize by making the string capacity somewhat larger than // it was before (up to 2 times bigger). Status my_string push_back(MY_STRING hMy_string, char item //Precondition: hMy string is the handle to a valid My_ string object. //Postcondition: Removes the last character of a string in constant time // Guaranteed to not cause a resize operation of the internal data. Returns // SUCCESS on success and FAILURE if the string is empty Status my_string_pop back(MY STRING hMy_string); //Precondition: hMy_string is the handle to a valid My_string object. //Postcondition: Returns the address of the character located at the given // index if the index is in bounds but otherwise returns NULL. This address // is not usable as a c-string since the data is not NULL terminated and is // intended to just provide access to the element at that index. char" my_string_at(MY STRING hMy_string, int index); //Precondition: hMy string is the handle to a valid My string object. //Postcondition: Returns the address of the first element of the string object // for use as a c-string. The resulting c-string is guaranteed to be NULL // terminated and the memory is still maintained by the string object though // the NULL is not actually counted as part of the string (in size). char* my_string_c_str(MY_STRING hMy_string)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