封装实现strlen函数
1 #include<stdio.h>
2
3 int MY_STRLEN(char *s)
4 {
5
6 char *p=s;
7 while(*(p++)!='\0')
8 {
9 p++;
10 }
11 return p-s-1;
12 }
13 int main()
14 {
15 char str[10]="abcde";
16 printf("%d\n",MY_STRLEN(str));
17 return 0;
18 }
这里是运行结果:
封装实现Strcmp函数
1 #include<stdio.h>
2
3
4 void aa(char *p,char *q)
5 {
6 for(int i=0;i<10;i++)
7 {
8 if(q[i]=='\0')
9 {
10 p[i]='\0';
11 break;
12 }
13 else
14 {
15 p[i]=q[i];
16 }
17 }
18 puts(p);
19
20 }
21
22
23 int main()
24 {
25 char str1[20]="hello";
26 char str2[10]="hi";
27 aa(str1,str2);
28 return 0;
29
30 }
这里是运行结果:
封装实现strlcat函数
1 #include<stdio.h>
2
3
4 void lianjie(char *p,char *q)
5 {
6 int i=0,j=0;
7 while(p[i]!='\0')
8 {
9 i++;
10 }
11 while(q[j]!='\0')
12 {
13 p[i]=q[j];
14 j++;
15 i++;
16 }
17 puts(p);
18
19 }
20
21 int main()
22 {
23 char str1[20]="hello";
24 char str2[10]="hi";
25 lianjie(str1,str2);
26
27 return 0;
28 }
这里是运行结果:
字符串二与字符串一连接在了一起,连接过程中先找到字符串一的’\0’位,让字符串二的首元素覆盖上去进行连接。