summaryrefslogtreecommitdiffstats
path: root/5/5.c
blob: f9156efe6ee3a9e5ca57f2ecc32db95b7f01e72d (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
31
32
33
34
35
36
37
38
39
40
#include <stddef.h>
#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) * MAXLEN);
  t = "hello, world!";

  mstrncpy(s, t, n);
  printf("mstrncpy: %s\n", s);

  t = "may the force be with you";
  mstrncat(s, t, 7);
  printf("mstrncat: %s\n", s);

  free(s);

  return 0;
}

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

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