Question: 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
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 kstrput(kstring str, size_t pos, char ch)
Replaces the character at index pos in str's data with the character ch.
If pos is out of bounds (greater than or equal to the length of str), this function should abort the program by calling abort()
Here are some other functions that I have already written:
int kstrlen(char kstring[]) // function to return length of string { int j = 0; while (kstring[j] != '\0') //using while loop for reading every character of string //because a string is terminated by a null character { ++j; } return j;
}
int kstrget(char kstring[],int pos) // function to return character at a specific index { int j = 0; while (kstring[j] != '\0') //using while loop for reading every character of string //because a string is terminated by a null character { ++j; } if(pos<=j+1) return kstring[pos-1]; //return character at specific index else abort(); //abort if index is out of bound }
void kstrput(kstring str, size_t pos, char ch) { if(pos>=str.length && pos<0) abort(); else *((str.data)+pos) = ch; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
