1.myStrnpy(str,p,9); //把p地址的前9位字符串拷贝到str这边来
#include <stdio.h>char* myStrcpy(char *des,char *src) //目标地址,原地址
{if(des == NULL || src == NULL){return NULL;}char *bak = des; //目标地址赋值给备份地址while(*src !='\0'){*des = *src;des++;src++;}*des = '\0';return bak;
}char* myStrcpy2(char *des,char *src) //目标地址,原地址
{if(des == NULL || src == NULL){return NULL;}char *bak = des; //目标地址赋值给备份地址while(*src !='\0'){*des++ = *src++;}*des = '\0';return bak;
}char* myStrcpy3(char *des,char *src) //目标地址,原地址
{if(des == NULL || src == NULL){return NULL;}char *bak = des; //目标地址赋值给备份地址while( (*des++ = *src++)!='\0'){ //赋值归赋值,是可以多一层判断的//赋值加判断 可以一起 }*des = '\0';return bak;
}char* myStrnpy(char *des,char *src,int count) //目标地址,原地址
{if(des == NULL || src == NULL) //判断目的地址和原地址不等于0{return NULL;}char *bak = des; //目标地址赋值给备份地址while(*src !='\0' && count>0){*des++ = *src++;count--;*des = '\0';}if(count >0) {while(count > 0){count--;*des++ = '\0'; //给多出来的位置赋值}}*des = '\0';return bak;
}int main()
{char str[128] = {'\0'};char *p = "caoshupei handsome";myStrcpy(str,p); //把p拷贝到str这边来puts(str);myStrcpy2(str,p);puts(str);myStrcpy3(str,p);puts(str);myStrnpy(str,p,9);puts(str);return 0;
}
2. 断言:assert ( ) 判断括号内其值为假(为0)则终止程序
#include "assert.h"
void assert(int experssion);
输出expression:在哪里错误
#include <stdio.h>
#include "assert.h"char* myStrcpy(char *des,char *src) //目标地址,原地址
{assert(des!= NULL && src != NULL);char *bak = des; //目标地址赋值给备份地址while(*src !='\0'){*des = *src;des++;src++;}*des = '\0';return bak;
}char* myStrcpy2(char *des,char *src) //目标地址,原地址
{if(des == NULL || src == NULL){return NULL;}char *bak = des; //目标地址赋值给备份地址while(*src !='\0'){*des++ = *src++;}*des = '\0';return bak;
}char* myStrcpy3(char *des,char *src) //目标地址,原地址
{if(des == NULL || src == NULL){return NULL;}char *bak = des; //目标地址赋值给备份地址while( (*des++ = *src++)!='\0'){ //赋值归赋值,是可以多一层判断的//赋值加判断 可以一起 }*des = '\0';return bak;
}char* myStrnpy(char *des,char *src,int count) //目标地址,原地址
{if(des == NULL || src == NULL) //判断目的地址和原地址不等于0{return NULL;}char *bak = des; //目标地址赋值给备份地址while(*src !='\0' && count>0){*des++ = *src++;count--;*des = '\0';}if(count >0) {while(count > 0){count--;*des++ = '\0'; //给多出来的位置赋值}}*des = '\0';return bak;
}int main()
{char str[128] = {'\0'};char *p = "caoshupei handsome";char *pstr = NULL;myStrcpy(pstr,p); //把p拷贝到str这边来myStrcpy(str,p); //把p拷贝到str这边来puts(str);myStrcpy2(str,p);puts(str);myStrcpy3(str,p);puts(str);myStrnpy(str,p,9);puts(str);return 0;
}