版本一
void My_strcpy(char* dest, const char* scr)//拷贝复制
{
assert(scr != nullptr&&dest !=nullptr);
int i = 0;
while (scr[i] != '\0')
{
dest[i] = scr[i];
++i;
}
dest[i] = '\0';//字符串最后都是'\0'
}
int main()
{
char stra[20] = { "zytzyt" };
char strb[20]= {};
char strc[20] = {};
My_strcpy(strb, stra);
printf("%s\n", strb);
return 0;
}
版本二
void My_strcpy(char* dest, const char* scr)
{
assert(scr != nullptr && dest != nullptr);
char* cp = dest;
while (*scr != '\0')
{
*cp = *scr;
cp++;
scr++;//这三句相当于 *cp++ = *scr
}
*cp = '\0';
}
版本三
void My_strcpy(char* dest, const char* scr)
{
assert(scr != nullptr && dest != nullptr);
char* cp = dest;
while (*cp++ = *scr)//赋值语句有返回值,返回的是赋值后的结果.只有0是flae,非0的数都是true
*cp = '\0';
}
char ch = ' \0 ' ;//flase
char ch = 0 ; //flase
char ch = ' 0 ' ;//true,字符0的ASCII值是48