1:实现strlen
4 int main(int argc, const char *argv[])
5 {
6 char s[30] = "hello world";
7 int i = 0;
8
9 while(s[i]!='\0')
10 {
11 i++;
12 }
13 printf("len = %d\n", i);
14 return 0;
15 }
运行结果:
2:实现strcpy
16 char s1[32] = "hello world";
17 char s2[32] = "hqyj";
18 printf("s1 = %s\n", s1);//hello world
19 printf("s2 = %s\n", s2);//hqyj
20 int i = 0;
21 while(s2[i] != '\0'){
22 s1[i] = s2[i];
23 i++;
24 }
25 //将 s2 的 '\0' 也复制给s1
26 s1[i] = s2[i];
27
28 printf("s1 = %s\n", s1);//hqyj
29 printf("s2 = %s\n", s2);//hqyj
30 return 0;
31 }
运行代码: