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

String Structs Description and Specifications You are going to build a program t

ID: 3853662 • Letter: S

Question

String Structs

Description and Specifications

You are going to build a program that creates your own version of a String structure. Your String structure (in String.h) should contain two members:

const char* text;

int size;

String Functions

You will need to define functions (prototypes in String.h, definitions in String.cpp) to accomplish the following tasks:

A find function that locates the next occurrence of a delimiting character starting at a specified index

int find (String* str, char delimiter, int start);

A substring function that extracts all the characters between a provided start and end indices of a passed String, creating a new String containing those characters

String* substr (string* str, int start, int end); //Inclusive

A compare function to compare two separate strings

int compare (String* str1, String* str2);

A createString function to create a new string and place the string in the structure variable. Use strlen to compute the length of the charArray. You will need to #include cstring.

String* createString(const char* charArray);

A destroyString function to release the memory of the string.

void destroyString (String* str);

A displayString function to print the string

void displayString (String* str);

Driver Program

Once you have created these functions, you will need to create a driver program (Lab12.cpp) that fully tests the functionality

Explanation / Answer

Here is the code for question. Post a comment in case of any issue. Please rate the answer if it helped. Thank you.

String.h

#ifndef String_h

#define String_h

typedef struct

{

char* text;

int size;

}String;

int find (String* str, char delimiter, int start);

String* substr (String* str, int start, int end); //Inclusive

int compare (String* str1, String* str2);

String* createString( const char* charArray);

void destroyString (String* str);

void displayString (String* str);

#endif /* String_h */

String.cpp

#include "String.h"

#include <cstring>

#include <iostream>

int find (String* str, char delimiter, int start)

{

for(int i = start , len = strlen(str->text); i < len ; i++)

if(str->text[i] == delimiter)

return i;

  

return -1; //when delimiter not found

}

//returns String* of the substring if valid values are passed, NULL if invalid inputs, if end is more then lenght of text in str,

//only available characters are copied.

String* substr (String* str, int start, int end)//Inclusive

{

//check that str is not NULL and start is a valid value

if(str != NULL && start >= 0 && start <= end && start < strlen(str->text))

{

String* substr = new String();

int len;

if(end < strlen(str->text))

len = end - start + 1;

else

len = strlen(str->text) - start ;

  

substr->text = new char[len +1];

substr->size = len;

strncpy(substr->text, str->text+start, len);

return substr;

}

else

return NULL;

}

int compare (String* str1, String* str2)

{

return strcmp(str1->text , str2->text);

}

String* createString(const char* charArray)

{

if(charArray == NULL)

return NULL;

String* str = new String();

str->size = strlen(charArray);

str->text = new char[str->size+1];

strcpy(str->text, charArray);

return str;

}

void destroyString (String* str)

{

delete str->text;

delete str;

}

void displayString (String* str)

{

std::cout << str->text;

}

Lab12.cpp

#include "String.h"

#include <iostream>

using namespace std;

int main()

{

char chstr[25] = "Good morning";

String *str1 = createString(chstr);

String *str2 = substr(str1, 2, 8); //start and end valid

String *str3 = substr(str1, 5, 15); //end index out of range

  

  

cout << "str1 = ";

displayString(str1);

cout << endl;

  

cout << "str2 = ";

displayString(str2);

cout << endl;

  

cout << "str3 = ";

displayString(str3);

cout << endl;

  

cout << "compare(str1, str2) = " << compare(str1, str2) << endl;

if(compare(str1, str2))

cout << "str1 < str2" << endl;

else if(compare(str1 , str2) > 0)

cout << "str1 > str2" << endl;

else

cout << "str1 = str2" << endl;

  

destroyString(str1);

destroyString(str2);

destroyString(str3);

  

}

output

str1 = Good morning

str2 = od morn

str3 = morning

compare(str1, str2) = -40

str1 < str2