can it be in programming language C, thanks 1. (20 pt) Implement a function char
ID: 3743171 • Letter: C
Question
can it be in programming language C, thanks
1. (20 pt) Implement a function char head_tail_trim(char s1, int h, int t)i which copies the characters (except the first h character and last t characters) from the given string s1 into a dynamically created new string and returns the pointer to this new string. If t+h is greater than the length of the string, or h or t is less than 0, return NULL For example After s- head tail trim("ABCDEFG", 2, 3) s should be pointing to "CD" After s head tail trim("ABCDEFG", 0, 3 s should be pointing to "ABCD" After s -head tail trim("ABCDEFG", 3, 4) should be pointing to "" After s-head tail trim("ABCDEFG", 5, 4 s should be NULL After s= head tailtrim ("ABCDEFG", -2, 3); s should be NULL. - Suppose standard libraries are included. So, standard library functions can be used if needed. char * head tail trim (char *s1, int h, int t) (/* you can use either pointer or array notation /Explanation / Answer
// C code
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
char *head_tail_trim(char *s1, int h, int t)
{
if( (h+t) > strlen(s1) || h < 0 || t < 0 || h > t)
return NULL;
char *result = malloc(sizeof(int)*(t-h+1));
int i = 0;
while(h <= t)
{
result[i] = s1[h];
i++;
h++;
}
return result;
}
int main()
{
char *s = head_tail_trim("ABCDEFG", 2, 3);
printf("%s ",s);
s = head_tail_trim("ABCDEFG", 0, 3);
printf("%s ",s);
s = head_tail_trim("ABCDEFG", 3, 4);
printf("%s ",s);
s = head_tail_trim("ABCDEFG", 5, 4);
printf("%s ",s);
s = head_tail_trim("ABCDEFG", -2, 3);
printf("%s ",s);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.