1 #include <stdio.h>
2 char* mystrcpy(char *dest,const char *src)
3 {
4 int i=0;
5 while(src[i]!='\0')
6 {
7 dest[i]=src[i];
8 i++;
9 }
10 dest[i]='\0';
11 return dest;
12 }
13 int mystrcmp(const char *s1,const char *s2)
14 {
15 int i=0,res=0;
16 while(s1[i]!='\0'||s2[i]!='\0')
17 {
18 res=s1[i]-s2[i];
19 if(res!=0)
20 {
21 return res;
22 break;
23 }else
24 {
25 return 0;
26 }
27 }
28 }
29 char *mystrcat(char *dest,const char *src)
30 {
31 int i=0,j,k;
32 while(dest[i]!='\0')
33 {
34 i++;
35 }
36 for(k=i,j=0;src[j]!='\0';k++,j++)
37 {
38 dest[k]=src[j];
39 }
40 dest[k]='\0';
41 return dest;
42 }
43 int mystrlen(const char *s)
44 {
45 int i=0;
46 while(s[i]!='\0')
47 {
48 i++;
49 }
50 return i;
51 }
52 int main(int argc, const char *argv[])
53 {
54 char str1[32]="hello";
55 char str2[32]="word";
56 // printf("%s\n",mystrcpy(str1,str2));
57 // printf("%s\n",mystrcat(str1,str2));i
58 printf("%d\n",mystrcmp(str1,str2));
59 printf("%d\n",mystrlen(str1));
60 return 0;
61 }
封装string函数
最新推荐文章于 2024-07-20 09:51:47 发布