The language is C Help is appreciated String manipulation fu
The language is C. Help is appreciated!
String manipulation functions are often vulnerable to informed C strings: character arrays that lack string terminators. For example, if copy String of Exercise 3.19 is given an ill-formed string as in, it will read and write through memory until a 0 is found or until a segmentation fault occurs. Write a protected version of copy String that transfers at most n - 1 characters from in two out and always writes a string terminator to out./* Copies at most n - 1 characters of string in into the * buffer pointed to buy out. If n is reached, returns - 2. *Otherwise, returns - 1 for malformed input and 0 upon */int copy String N (char * in, char * out, int n); Implement a unit test of copy String N in a main function that exercises its full protective functionality.Solution
#include <stdio.h>
int copyStringN(char *in, char *out, int n){
int i=0;
if(n < 0){
return -1;
}
for(i=0; i<n; i++){
out[i]=in[i];
}
return 0;
}
int main()
{
char in[7] =\"testing\";
char out[7];
copyStringN(in, out, 7);
printf(\"Output string value is %s\ \", out);
return 0;
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Output string value is testing
