Copy the string

#include<stdio.h>
/int main()
{
char str1[10]=“abcd”;
char str2[10];
int i=4;
for(int i=0;str1[i]!=’\0’;i++)
{
str2[i]=str1[i];
}
str2[i]=’\0’;
printf(“str1=%s,str2=%s\n”,str1,str2);
}
/
//用数组
void mystrcpy1(char *desstr,char *srcstr)
{int i;
for(i=0;srcstr[i]!=’\0’;i++)
{
desstr[i]=srcstr[i];
}
desstr[i]=’\0’;
}
//用指针
void mystrcpy2(char *desstr,char srcstr)
{
int i;
for(i=0;
(srcstr+i);i++)
{
(desstr+i)=(srcstr+i);
}
* (Desstr + I) = '\ 0';
}
// pointer down their
void mystrcpy3 (desstr char *, char * srcstr)
{
the while (! * Srcstr = '\ 0')
{
* * = desstr srcstr;
* desstr ++;
* srcstr ++;
}
* desstr = '\ 0';
}
// ultimate embodiment
void mystrcpy4 (char * desstr, char * srcstr)
{
the while (* srcstr = '\ 0'!)
{
* desstr ++ = * srcstr ++; // Key statement
}
* desstr = '\ 0';
}

int main ()

{
Char Ha [10];
STRB char [10];
bunchy char [10];
honey char [10];
STR1 char [] = "abcde";
STR2 char [] = "ABCDEF";
STR3 char [] = "savvas";
str4 char [] = "apple";
mystrcpy1 (rear, STR1);
mystrcpy2 (STRB, STR2);
mystrcpy3 (STRC, STR3);
mystrcpy4 (honey, str4);
printf ( "% s STR1 = \ n, rear =% s \ n", STR1, rear);
printf ( "STR2 =% s \ n STRB =% s \ n", STR2, STRB);
printf ( "% s STR3 = \ n, STRC =% s \ n", STR3, STRC);
printf ( "% s STR4 = \ n, honey =% s \ n", STR4, honey);
}

Here Insert Picture Description
str [0] = str
str [1] =
(str + 1)
str [-1] = (str-1)
str [i] =
(str + i)

Published 13 original articles · won praise 3 · Views 634

Guess you like

Origin blog.csdn.net/weixin_43873172/article/details/88141748