admin 管理员组

文章数量: 1087139


2024年2月19日发(作者:宝可梦属性克制关系)

// 有一个字符数组a,在其中存放字符串“I am a boy.”,要求把该字符串复制到字符数组b中。

/*

#include

int main()

{

char a[]="I am a boy.";

char b[20];

int i;

for(i=0;*(a+i)!='0';i++)

{

*(b+i)=*(a+i);

}

*(b+i)='0';

printf("string a is: %sn",a);

printf("string b is:");

// for(i=0;b[i]!='0';i++)

for(i=0;*(b+i)!='0';i++)

{

// printf("%c",b[i]);

printf("%c",*(b+i));

}

printf("n");

return 0;

}

*/

//用指针变量来实现

/*

#include

int main()

{

char a[]="I am a boy.";

char b[20],*p1,*p2;

int i;

p1=a;

p2=b;

for(;*p1!='0';*p1++,*p2++) {

*p2=*p1;

}

*p2='0';

printf("sting a is:%sn",a);

printf("string b is:");

// 用地址法访问数组元素

// for(i=0;b[i]!='0';i++)

for(i=0;*(b+i)!='0';i++)

{

// printf("%c",b[i]);

printf("%c",*(b+i));

}

printf("n");

return 0;

}

*/

// 用函数调用来实现

#include

int main()

{

void copy_string(char *from,char *to);

char *a="I am a teacher.";

字符串

char b[]="You are a student.";

char *p=b;

的首元素

printf("string a=%snstring b=%sn",a,p); printf("ncopy string a to string b:n");

copy_string(a,p);

printf("string a=%snstring b=%sn",a,b); return 0;

}

/*

void copy_string(char *from,char *to)

{

for(;*from!='0';from++,to++);

{

*to=*from;

}

*to='0';

}

*/

/*

void copy_string(char *from,char *to)

{

while((*to=*from)!='0')

{

to++;

from++;

// 定义a为字符指针变量,指向一个 // 定义b为字符数组,内放一个字符串 // 字符指针变量p指向字符数组b // 用字符串做形参

// 形参是字符指针变量

// 只要a串没结束就复制到b数组

}

}

*/

/*

void copy_string(char *from,char *to)

{

while((*to++=*from++)!='0');

}

*/

/*

void copy_string(char *from,char *to)

{

while(*from!='0')

*to++=*from++;

*to='0';

}*/

void copy_string(char *from,char *to)

{

char *p1,*p2;

p1=from;

p2=to;

while((*p2++=*p1++)!='0');

}


本文标签: 字符 数组 指针 变量 字符串