C/C++教程

第五阶段—函数—几个特殊函数—字符串操作函数:编写函数mystrcpy

本文主要是介绍第五阶段—函数—几个特殊函数—字符串操作函数:编写函数mystrcpy,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
 1 #include<stdio.h>
 2 #include<assert.h>
 3 #include<string.h>
 4 
 5 #define N 20
 6 char *my_strcpy(char *dest, char *src)
 7 {
 8     assert((dest != NULL)&&(src != NULL));//断言:判断表达式一定为真,若为假,则终止程序    
 9     char  *p = dest;
10     while(*dest = *src)
11     {
12         if(*src == '\0')
13             break;
14         dest++;
15         src++;
16     }
17     return p;
18 }
19 
20 int main(int argc, const char *argv[])
21 {
22     char buf[N] = {"abcdefgtyui"};//定义需要传给形参,给形参赋值的变量
23     char buff[] = {"12345"};
24     char *p = NULL;
25     int i = 0;
26     p = my_strcpy(buf, buff);
27     for(i = 0;i< N;i++)
28     {
29         //printf("%c ",*(p + i));
30         printf("%c ",p[i]);
31     }
32     printf("strcpy(buf, buff) = %s\n", p);
33     p = strcpy(buf, buff);
34     printf("strcpy(buf, buff) = %s\n", p);
35     
36     putchar('\n');
37     return 0;
38 }

 

这篇关于第五阶段—函数—几个特殊函数—字符串操作函数:编写函数mystrcpy的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!