第五阶段—函数—几个特殊函数—字符串操作函数:编写函数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
// 6 char *my_strcpy(char *dest, char *src)
// 7 {
// 8 assert((dest != NULL)&&(src != NULL));//断言:判断表达式一定为真,若为假,则终止程序
// 9 char *p = dest;
// 10 while((*dest++ = *src++) != 0);
// 17 return p;
// 18 }
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 }
浙公网安备 33010602011771号