Question

Working on a program where my task is to implement a library (as a set of...

Working on a program where my task is to implement a library (as a set of source and header files, documentation, and a Makefile) that provides a few useful functions for working with a counted string data structure.

And one of the functions that needs to be written is described below but I am having a little difficulty writing it. Program needs to be written in C.

Struct:

typedef struct
{
   char *data;
   size_t length;
} kstring;

function:

void kstrextend(kstring *strp, size_t nbytes)

Modifies an existing kstring, pointed to by strp, to be at least nbytes bytes long.

If strp->length was already nbytes or longer, does nothing. That is, this function will never reduce the length of a string, only increase it.

If nbytes is longer than the current length, this function should take the following steps:

  1. Allocate a new array with the larger size.
  2. Copy data over from the old array to the new one.
  3. Free the old array.
  4. Fill the additional elements of the new array with null bytes ('\0').
  5. Make strp->data point to the new array.
  6. Set strp->length to the new size.

Note: if you are using malloc() for memory allocation, the function realloc() (Links to an external site.)Links to an external site. will take care of steps 1, 2, and 3 with a single function call.

If there is an error allocating memory, this function should call abort().

Note that this function takes a pointer to a kstring rather than taking a kstring by value. That means that changes you make to the strp->length and strp->data members will be visible to the caller.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I used malloc, so that you have a better understanding of how the process works.

Malloc has a void return type, so you need to typecast it to the required datatype after memory is allocated.

Neverthless, if it fails to allocate memory, it will return NULL. If so, we call abort().

//REST OF THE EXPLANATION IN COMMENTS

#include<stdio.h>
typedef struct
{
char *data;
size_t length;
} kstring;

void kstrextend(kstring *strp, size_t nbytes){
if(strp->length>=nbytes)
return;
char *p;
p=(char*)malloc(nbytes*sizeof(char));
if(p==NULL)abort();
int i=0;//iterates till it reaches null character of current array
while(strp->data[i]!='\0')
{

//copies current character of original array
p[i]=strp->data[i];
i++;
}
//already copied the stuff
//now we fill up the remaining section
while(i<nbytes){
//appending null strings to remaining character
p[i]='\0';
i++;
}
//frees the previous array
free(strp->data);
//changes pointer to new attributes
strp->data=p;
strp->length=nbytes;
}
void main()
{
kstring name;
name.data="Chegg";
name.length=5;
kstrextend(&name,40);
printf("%s %d",name.data,name.length);
}

Add a comment
Know the answer?
Add Answer to:
Working on a program where my task is to implement a library (as a set of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Working on a program where my task is to implement a library (as a set of...

    Working on a program where my task is to implement a library (as a set of source and header files, documentation, and a Makefile) that provides a few useful functions for working with a counted string data structure. And one of the functions that needs to be written is described below but I am having a little difficulty writing it. Program needs to be written in C. void kstrcat(kstring *destp, kstring src) Concatenates str onto the end of *destp. First...

  • Implement a C program unique.c that recreates the functionality of the uniq tool by reading the...

    Implement a C program unique.c that recreates the functionality of the uniq tool by reading the input line by line and dropping each l ine that is identical to the line immediately before it. On the input abc abc abc your program should output abc abc the same output that uniq would produce. While not strictly necessary for this step, it is important as a basis for subsequent steps that you read the entire input and store it as a...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity =...

    How to use C to output this: indigo1 376 % lab9 Capacity = 4 Capacity = 8 0 5 10 15 20 Capacity = 16 0 5 10 15 20 25 30 35 40 Capacity = 32 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

  • I have updated my previously posted C++ question to ensure that I have included all of...

    I have updated my previously posted C++ question to ensure that I have included all of the necessary documentation in order to complete Part 2 - Bank Account of the assigned lab in its entirety. Please note that the lab below is somewhat lengthy, and I do not expect you to answer each question! If coding the full lab is too much to ask, please focus soley on Part 2 of the lab, titled Bank Account and the subsections that...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT