-
Notifications
You must be signed in to change notification settings - Fork 612
/
C++_Strcpy.cpp
61 lines (51 loc) · 1.3 KB
/
C++_Strcpy.cpp
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
/*
* @Author: [email protected]
* @Last Modified time: 2016-08-20 16:00:39
*/
#include <stdio.h> // printf
#include <string.h> // strlen
#include <cstdlib> // malloc
char* char_memcpy(char * destination, const char *source, int len){
if(destination==NULL || source==NULL){
return NULL;
}
char *ret = destination;
if(destination >= source && destination < source+len){
destination += len-1;
source += len-1;
while(len--){
*destination-- = *source--;
}
}
else{
while (len--) *destination++ = *source++;
}
return ret;
}
// Can process overlap now.
char* strcpy(char * destination, const char * source ){
if(destination==NULL || source==NULL){
return NULL;
}
char *des = destination;
char_memcpy(destination, source, strlen(source)+1);
return des;
}
int main() {
// Normal
char str[]="hello";
char* dest = (char *)malloc(10);
strcpy(dest, str);
printf("%s \n", dest); // hello
// Overlap 1
char s[]="hello";
strcpy(s+1, s);
printf("%s \n", s); // hhello
char ss[]="hello";
strcpy(ss+5, ss);
printf("%s \n", ss); // hellohello
// Overlap 2
char sss[]="hello";
strcpy(sss, sss+1);
printf("%s \n", sss); // ello
}