4,070 views
32 votes
32 votes
This is in the C

Write a function called my_str_n_cat() that accepts pointer to a destination character array, a pointer to a source character array (which is assumed to be a string), and a integer n, and returns the pointer to the destination character array. This function needs to copy at most n characters, character by character, from the source character array to the end of the destination character array. If a null character is encountered before n characters have been encountered, copying must stop. You may NOT use any functions found in to solve this problem! Note: you may use array or pointer notation in this function.

User Nitzan Volman
by
2.6k points

1 Answer

6 votes
6 votes

Please don't forget to edit the strings before compiling.

#include <stdio.h>

static int idx = 0;

#define SIZE 1000

char* my_str_n_cat(char* d, char* s, int n) {

while (*d!='\0') {

d++;

}

while (*s!='\0' && idx<n) {

*d = *s;

d++; s++; idx++;

}

*d='\0';

return d;

}

int main() {

char string[SIZE] = "First+";

char* string_2 = "Second";

int tmp; printf("Enter amount: "); scanf("%d",&tmp);

my_str_n_cat(string, string_2, tmp);

printf("\\%s\\",string);

return 0;

}

This is in the C Write a function called my_str_n_cat() that accepts pointer to a-example-1
User Ezero
by
2.8k points