#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* a structure that defines a string buffer */
typedef struct strbuf {
char *contents; /* pointer to dynamically allocated buffer contents */
size_t length; /* number of characters in buffer (excluding '\0') */
} StringBuffer;
/* function prototypes */
StringBuffer *strbuf_new(const char *cstr);
StringBuffer *strbuf_append(StringBuffer *buf, const char *cstr);
StringBuffer *strbuf_insert(StringBuffer *buf, const char *cstr, size_t pos);
StringBuffer *strbuf_reset(StringBuffer *buf, const char *cstr);
void strbuf_free(StringBuffer *buf);
int main(void)
{
StringBuffer *sb = NULL;
sb = strbuf_new("Hello");
if (sb == NULL)
printf("*** Failed to create buffer");
else {
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
if (strbuf_append(sb, " world") == NULL)
printf("*** Failed to append\n");
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
if (strbuf_reset(sb, NULL) == NULL)
printf("*** Failed to reset\n");
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
if (strbuf_append(strbuf_append(strbuf_append(sb, "I"), " am"), " Sam") == NULL)
printf("*** Failed to append\n");
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_free(sb);
}
sb = strbuf_new(NULL);
if (sb == NULL)
printf("*** Failed to create buffer");
else {
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
if (strbuf_append(sb, "aaaaa") == NULL)
printf("*** Failed to append\n");
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
if (strbuf_insert(sb, "bbb", 2) == NULL)
printf("*** Failed to insert\n");
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
if (strbuf_reset(sb, "aaaaa") == NULL)
printf("*** Failed to reset\n");
if (strbuf_insert(sb, "bbb", 0) == NULL)
printf("*** Failed to insert\n");
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
if (strbuf_reset(sb, "aaaaa") == NULL)
printf("*** Failed to reset\n");
if (strbuf_insert(sb, "bbb", 10) == NULL)
printf("*** Failed to insert\n");
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
if (strbuf_insert(strbuf_insert(strbuf_reset(sb, "aaa"), "bbb", 1), "ccc", 5) == NULL)
printf("*** Failed to reset/insert\n");
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_free(sb);
}
// the following calls are meant to fail
if (strbuf_reset(NULL, "stuff") == NULL)
printf("*** Failed to reset\n");
if (strbuf_append(NULL, "stuff") == NULL)
printf("*** Failed to append\n");
if (strbuf_insert(NULL, "stuff", 0) == NULL)
printf("*** Failed to insert\n");
return 0;
}
/*
int main(void)
{
StringBuffer *sb = NULL; // buffer does not exist yet
sb = strbuf_new("Hello"); // new buffer allocated containing "Hello"
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_append(sb, " world"); // buffer contains "Hello world"
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_reset(sb, NULL); // buffer contains empty string ""
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_append(strbuf_append(strbuf_append(sb, "I"), " am"), " Sam"); // buffer contains "I am Sam"
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_free(sb); // buffer deallocated (contents no longer accessible)
sb = strbuf_new(NULL); // new buffer allocated containing empty string ""
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_append(sb, "aaaaa"); // buffer contains "aaaaa"
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_insert(sb, "bbb", 2); // buffer contains "aabbbaaa"
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_reset(sb, "aaaaa"); // buffer contains "aaaaa"
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_insert(sb, "bbb", 0); // buffer contains "bbbaaaaa"
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_reset(sb, "aaaaa"); // buffer contains "aaaaa"
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_insert(sb, "bbb", 10); // buffer contains "aaaaabbb"
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_insert(strbuf_insert(strbuf_reset(sb, "aaa"), "bbb", 1), "ccc", 5); // buffer contains "abbbaccca"
printf("length = %2u: \"%s\"\n", (unsigned)sb->length, sb->contents);
strbuf_free(sb); // buffer deallocated (contents no longer accessible)
return 0;
}
*/
/*****************************************************************************/
/****************** ***************************/
/**************** IMPLEMENT THE FUNCTIONS HERE *************************/
/***************** ***************************/
/*****************************************************************************/
StringBuffer *strbuf_new(const char *cstr){
StringBuffer *strbuf = (StringBuffer*) malloc(sizeof(StringBuffer));
if(strbuf == NULL){
return NULL;
}
else{
if(cstr != NULL){
char *str = (char*) malloc(sizeof(char) * (strlen(cstr) + 1));
if(str == NULL){
return NULL;
}
else{
strcpy(str,cstr);
strbuf->contents = str;
strbuf->length = strlen(str);
}
}
else{
char *blankstr = (char*) malloc(sizeof(char));
if(blankstr == NULL){
return NULL;
}
else{
strbuf->contents = blankstr;
strcpy(strbuf->contents,"");
strbuf->length = strlen(strbuf->contents);
}
}
}
return strbuf;
}
StringBuffer *strbuf_append(StringBuffer *buf, const char *cstr){
if(buf == NULL){
return NULL;
}
if(cstr == NULL){
return buf;
}
else{
char *newstr = (char*) realloc(buf->contents, buf->length + strlen(cstr));
if (newstr == NULL){
return NULL;
}
else {
buf->contents = newstr;
strcat(buf->contents, cstr);
buf->length = strlen(buf->contents);
}
}
return buf;
}
StringBuffer *strbuf_insert(StringBuffer *buf, const char *cstr, size_t pos){
if(buf == NULL){
return NULL;
}
if(cstr == NULL){
return buf;
}
else{
char *newstr = (char*) realloc(buf->contents, buf->length + strlen(cstr));
if (newstr == NULL)
return NULL;
else {
if(pos >= buf->length){
buf->contents = newstr;
strcat(buf->contents, cstr);
buf->length = strlen(buf->contents);
}
else{
buf->contents = newstr;
strcat(buf->contents, cstr);
buf->length = strlen(buf->contents);
}
}
}
return buf;
}
StringBuffer *strbuf_reset(StringBuffer *buf, const char *cstr){
if(buf == NULL){
return NULL;
}
if(cstr == NULL){
char *newstr = (char*) malloc(sizeof(char));
if(newstr == NULL){
return NULL;
}
else{
buf->contents = newstr;
strcpy(buf->contents,"");
buf->length = strlen(buf->contents);
}
}
else{
char *newstr = (char*) realloc(buf->contents, strlen(cstr));
if (newstr == NULL){
return NULL;
}
else {
buf->contents = newstr;
strcpy(buf->contents, cstr);
buf->length = strlen(buf->contents);
}
}
return buf;
}
void strbuf_free(StringBuffer *buf){
if(buf != NULL){
free(&buf->length);
free(&buf->contents);
free(buf);
}
}
OK, so the problem is that the program crashes on line 41, which to me makes no sense because I have already successfully called that function (line 22). If I comment that line out it does not crash ! I have tested everything I can think of, and yes it is a homework assignment and it is due tomorrow (Friday July 2nd, by 11:59pm). PLEASE HELP ME ! I am very lost.