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.

void kstrcat(kstring *destp, kstring src)

Concatenates str onto the end of *destp. First extends *destp to be long enough to hold the contents of both strings, by calling kstrextend(). Then copies the contents of src into the dest

So, for example, if we execute the following code:

      kstring a = kstrfrom("hello"); // 6 bytes including \0
      kstring b = kstrfrom("world"); // 6 bytes including \0
      kstrcat(&a, b);

Now a should have length 12, and the contents "hello\0world\0".

Note that this function takes a pointer to the destination kstring. That means that changes you make to the destp->length and destp->data members will be visible to the caller.

Note: Remember to save the original length of *destp before calling kstrextend(). Otherwise, you won't know where to copy src's data to!

#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;
}

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

C code:

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


kstring kstralloc(size_t nbytes){

int i;
  
kstring new_kstring; // creating a object of type ksting
  
new_kstring.data =(char*) malloc(nbytes); // allocating nbytes of memory to create string of length nbytes
  
if (new_kstring.data == NULL && nbytes!= 0){
printf("There is an error allocating memory ");
abort();
}
  
new_kstring.length = (int)nbytes; // setting length member to size mentioned using nbytes
  
// initializinf by filling it with nbytes copies of the null byte ('').
for (i= 0;i<nbytes;i++)
new_kstring.data[i] = '';   
  
return new_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]!='')
{

//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]='';
i++;
}
//frees the previous array
free(strp->data);
//changes pointer to new attributes
strp->data=p;
strp->length=nbytes;
}


void kstrcat(kstring *destp, kstring src)
{
   size_t l=destp->length;
   kstrextend(destp,destp->length+src.length);
   destp->length=l+src.length;
   int i;
   for(i=l;i<l+src.length;i++)
   {
       destp->data[i]=src.data[i-l];
   }
   destp->data[i]='';
  
}

int main()
{
size_t i = 5;
  
kstring kstr1 = kstralloc(i); // Allocating memory for data member

strcpy(kstr1.data, "Hello");
printf("Data : %s Lenght : %d",kstr1.data,kstr1.length);


   kstring kstr2 = kstralloc(5); // Allocating memory for data member
strcpy(kstr2.data, "World");
printf(" Data : %s Lenght : %d",kstr2.data,kstr2.length);

kstrcat(&kstr1,kstr2);
printf(" Data : %s Lenght : %d",kstr1.data,kstr1.length);
}

output:

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. Struct: typedef struct {    char *data;    size_t length; } kstring; function:...

  • 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...

  • Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(...

    Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(char *s) {    /* this is here so code compiles */ return 0; } /* array version */ /* concantenate t to the end of s; s must be big enough */ void str_cat(char s[], char t[]) {    int i, j;    i = j = 0;    while (s[i] != '\0')    /* find end of s */        i++;    while ((s[i++] = t[j++]) != '\0') /* copy t */        ;...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • 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...

  • I am failing one of the tests cases for the trimArray function. The test that my...

    I am failing one of the tests cases for the trimArray function. The test that my function is failing is testing that when you get a toTrim array of  " ", it should return "" (an array of no characters). How do I incorporate that functionality into the function and where will it go in the C++ code? Here's my function: // The function trimArray() is given a pointer to a character array that // is NULL terminated called toTrim. This...

  • Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing...

    Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing two functions that will output the number of vowels and consonants in a user inputted string. We’ll be using functions from the cstring library, so be sure to include it! We’ll be calling those functions method_one and method_two. To start off, declare a character array (with no size) called vowels and initialize it with “aeiouyAEIOUY” in our main function. Declare a character array with...

  • Hello, I have my piece of C programming code and I have a pointer initialized to...

    Hello, I have my piece of C programming code and I have a pointer initialized to point to my array and I need help to display the contents of my array using a while loop or do-while loop. The stop condition should be when the pointer reaches '\0'. The code is below: #include <stdio.h> int main () {    char array[80]; printf("Enter a string: "); scanf("%[^\n]", &array); printf("%s\n", array); char * p;    p = array;         }

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

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