-
Notifications
You must be signed in to change notification settings - Fork 0
/
strcpy.c
91 lines (74 loc) · 1.61 KB
/
strcpy.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
// NOTE-1: copies the content from 'from' variable to
// 'to' variable
//
// NOTE-2: it is assumed that the 'to' variable is
// always big enough to hold the contents from the
// 'from' variable
//
// NOTE-3: strcpy returns the pointer (char *) to the
// destination string (to)
// array version
char * strcpy1(char to[], char from[])
{
int i=0;
while(from[i]) {
to[i] = from[i];
i++;
}
to[i] = '\0';
return to;
}
// pointer version
char * strcpy2(char *to, char *from)
{
/*
int i=0;
while (*from) {
*(to+i) = *from;
from++;
i++;
}
return to;
*/
char *t = to;
while (*to++ = *from++);
return t;
}
int strlen(char *p)
{
char *q = p;
while (*p)
p++;
return p - q;
}
int main()
{
#define size 25
char from[size] = "test string";
char to[size] = "";
char *fromp = "another string";
char *top = NULL;
top = (char *) malloc(sizeof(char) * strlen(fromp) + 1);
printf("strcpy1(%s, %s) = ", to, from);
printf("%s\n", strcpy1(to, from));
printf("strcpy1(%s, %s) = ", top, fromp);
printf("%s\n", strcpy1(top, fromp));
free (top);
// ------------------------------- //
char from1[size] = "hello, test test";
char to1[size] = "";
fromp = "diff string";
top = NULL;
top = (char *) malloc(sizeof(char) * strlen(fromp) + 1);
printf("strcpy2(%s, %s) = ", to1, from1);
printf("%s\n", strcpy2(to1, from1));
printf("strcpy2(%s, %s) = ", top, fromp);
printf("%s\n", strcpy2(top, fromp));
/*
printf("strcpy2(%s, %s) = %s \n", to, from, strcpy2(to, from));
printf("strcpy2(%s, %s) = %s \n", top, fromp, strcpy2(top, fromp));
*/
return 0;
}