Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need help in my lab. Evil Hangman This semester we will be building a project

ID: 3890873 • Letter: I

Question





i need help in my lab.

Evil Hangman This semester we will be building a project that plays the game of Hangman in a strange way. In order to begin this process we need to build a working string type for ourselves using opaque objects. Your task for this week is to set up the project makefile area and write a few initialization functions, the destroy function, and a working string compare function as well as writing some test code to test all of these operations. The lab assumes that you have completed all steps involved in lab-1 Switch to your HANGMAN directory cd /Summer2016/COMP1020/HANGMAN Clean up your directory by typing make clean Modify your Makefile so that it makes an executable named string_driver instead of one named hello. You will have to also modify the rule for clean so that it removes the correct file instead of the executable hello We need some additional files for our string type. Create a my_string.h interface file and a my_string.c implementation file. Create rules in your Makefile for the following: 1. You need a rule for a my_string.o object file which depends on my_string.h and my_string.c 2. Add my_string.o to the list of objects in the OBJECTS variable. You should be able to build the project using make even though the new files are completely empty. Your directory should contain the following files after you run make dbadams@cs3:/Summer2016/COMP1020/HANGMAN$ Is main.c main.o Makefile my_string.c my_string.h my_string.o string driver Verify that your "make clean" still cleans up all of the object files. Make the project again and then type make once more so that you get the message: make: string driver is up to date Type "touch my_string.h" and hit enter. Then type make again. This should force a recompile of my_string.o and, because of the dependency on my_string.o, string_driver. Notice how main.o did not need to be recompiled because we have not yet added a dependency on my_string.h for main.o. We are now ready to begin writing our own string library. We will begin by making a handle type and the bookends, an init_default function for my_string and a destroy function

Explanation / Answer

Given below are the required files. You can compile using

gcc my_string.c test.c

run using ./a.out

Please copy over the comments from your pdf for each of the functions in the .h file. The capacity and size also include the .

Hope the answer helps. If it did, please don't forget to rate the answer . Thank you.

my_string.h

==========


#ifndef my_string_h
#define my_string_h
#include <string.h>

typedef void* MY_STRING;

struct my_string
{
char *value;
int size;
int capacity;
};


MY_STRING my_string_init_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);

#endif /* my_string_h */

my_string.c

==========

#include "my_string.h"
#include <stdlib.h>

MY_STRING my_string_init_default(void)
{
struct my_string* ptr = (struct my_string*) malloc(sizeof(struct my_string));
if(ptr == NULL)
return NULL;
  
ptr->capacity = 7;
ptr->size = 0;
ptr->value = malloc(sizeof(char) * ptr->capacity); //default capacity of 7
if(ptr->value == NULL) //allocation failure
ptr->capacity = 0;
  
ptr->value[0] = ''; //empty string
return ptr;
}

void my_string_destroy(MY_STRING* phMy_string)
{
if(*phMy_string != NULL)
{
free(((struct my_string*)(*phMy_string))->value); //free the char array
free(*phMy_string); //free the struct
*phMy_string = NULL;
}
}

MY_STRING my_string_init_c_string(const char* c_string)
{
struct my_string* ptr = (struct my_string*) malloc(sizeof(struct my_string));
if(ptr == NULL)
return NULL;
  
ptr->capacity = strlen(c_string) + 1;
ptr->size = ptr->capacity;
ptr->value = malloc(sizeof(char) * ptr->capacity);
if(ptr->value == NULL) //allocation failure
ptr->capacity = ptr->size = 0;
  
strcpy(ptr->value, c_string);
return ptr;

}

int my_string_get_capacity(MY_STRING hMy_string)
{
return ((struct my_string*) hMy_string)->capacity;
}


int my_string_get_size(MY_STRING hMy_string)
{
return ((struct my_string*) hMy_string)->size;
}

int my_string_compare(MY_STRING hLeft_string, MY_STRING hRight_string)
{
char *s1 = ((struct my_string*) hLeft_string)->value;
char *s2 = ((struct my_string*) hRight_string)->value;
return strcmp(s1, s2);
}

test.c

=====


#include <stdio.h>
#include <stdlib.h>
#include "my_string.h"

int main(int argc, char* argv[])
{
MY_STRING str1 = NULL;
MY_STRING str2 = NULL;
  
int cmp;
  
str1 = my_string_init_c_string("hello");
str2 = my_string_init_c_string("hi");
  
printf("str1 capacity = %d ", my_string_get_capacity(str1));
printf("str1 size = %d ", my_string_get_size(str1));
  
cmp = my_string_compare(str1, str2);
if(cmp < 0)
printf("str1 is less than str2 ");
else if(cmp > 0)
printf("str1 is greater str2 ");
else
printf("str1 is equal to str2 ");
  
my_string_destroy(&str1);
my_string_destroy(&str2);
  
return 0;
}

output

str1 capacity = 6

str1 size = 6

str1 is less than str2