aboutsummaryrefslogtreecommitdiff
path: root/5.5.c
blob: bd14a40c70246956db0d509b8c69b6fcecc32d35 (plain) (blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>

char *strncpy(char to[], const char from[], int till);
char *strncat(char to[], const char from[], int till);
int strncmp_sneed(const char str1[], const char str2[], int till);

int 
main(void)
{
  char str[10];
  printf("%s\n", strncpy(str, "gnu linux", 3));

  printf("%s\n", strncat(str, " linux", 6));

  if (!strncmp_sneed("seed", "seed feed", 3))
    printf("suckses\n");

  return 0;
}

char *strncpy(char to[], const char from[], int till)
{
  if (from[till] != '\0')
    to[till+1] = '\0';

  while (till--)
    *(to+till) = *(from+till);

  return to;
}

char *strncat(char to[], const char from[], int till)
{
  char *to_og = to;

  while (*to)
    ++to;

  while (till--)
    *(to+till) = *(from+till);

  return to_og;
}

int 
strncmp_sneed(const char str1[], const char str2[], int till)
{
  int count;

  for (count = 0, --till; count < till && *(str1+count) == *(str2+count); count++)
    ;

  return *(str1+count) - *(str2+count);
}