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

Write a C function with the following prototype: int resize (char s[], int array

ID: 3748947 • Letter: W

Question

Write a C function with the following prototype:

int resize (char s[], int array_size, int new_size);

You function should change the size of the C-string stored in the array s. If new_size is shorter than the current length of the string, truncate the string. If new_size is longer than the current length of the string, pad the end of the string with spaces, but don't exceed the size of the array, which is stored in array_size. Return the new size of the string as the value of the function. Please be thorough with comments.

Explanation / Answer


int resize (char s[], int array_size, int new_size){
int len = strlen(s);
int i;
if(new_size >=0 ){ //make sure new_size is valid
if(new_size < len) //shorter, should truncate
{
s[new_size] = '';
}
else
{
//expand
if(new_size >= array_size) //check if it beyond the array_size
new_size = array_size - 1;
//from the old length of string till new_size , pad with spaces
for(i = len; i < new_size; i++)
s[i] = ' ';
s[new_size] = '';//terminate with null char
}
}
return new_size;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote