summaryrefslogtreecommitdiffstats
path: root/5/5.c
blob: 0c35198f6d6910d02100a65cfbb5fe4c834d0a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 1000

void mstrncpy(char *s, char *t, int n);
void mstrncat(char *s, char *t, int n);
void mstrncmp(char *s, char *t, int n);

int main(int argc, char *argv[]) {
  int n;
  char *s, *t;

  n = 5;
  s = malloc(sizeof(char) * (n + 1));
  t = "hello, world!";
  
  mstrncpy(s, t, n);
  printf("strncpy: %s\n", s);
  
  free(s);

  return 0;
}

void mstrncpy(char *s, char *t, int n) {
  for (; n > 0 && (*s++ = *t++) != 0; n--)
    ;
}