forked from wg135/data-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mystrcat.c
61 lines (41 loc) · 961 Bytes
/
mystrcat.c
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>
#include <stdlib.h>
#include <string.h>
char *my_strcat(char *dest, const char *src);
char *my_strncat(char *dest, const char *src, int n);
int main(int argc, char *argv[]){
char *a = "hello world";
char *b = " yes";
char c[50] = "\0";
my_strcat(c, a);
printf("%s\n", c);
my_strncat(c, b, 2);
printf("%s\n", c);
return 0;
}
char *my_strcat(char *dest, const char *src){
char *address;
if((dest == NULL)||(src == NULL)){
printf("NULL address\n");
exit(1);
}
address = dest;
while(*dest != '\0')
++dest;
while((*dest++ = *src++) != '\0')
;
return address;
}
char *my_strncat(char *dest, const char *src, int n){
char *address;
int i = 0;
if((dest == NULL)||(src == NULL)){
printf("NULL address\n");
exit(1);
}
address = dest;
while(*dest != '\0')
++dest;
while(((*dest++ = *src++) != '\0')&&(++i <n));
return address;
}