C程序设计(第四版) 谭浩强 例8.18 个人设计
例 8.18 将字符串a复制为字符串b, 然后输出字符串b。
代码块:
方法1:
#include <stdio.h>
int main()
{
char *a="I love you!", *b="I hate you!";
printf("Original string: a=%s, b=%s\n", a, b);
b=a;
printf("New string: a=%s, b=%s\n", a, b);
return 0;
}
方法2:
#include <stdio.h>
int main()
{
char a[]="I love you!", b[]="I hate you!", *p, *q;
printf("Original string: a=%s, b=%s\n", a, b);
for (p=a, q=b; *p!='\0'; *q++=*p++);
*q='\0';
printf("New string: a=%s, b=%s\n", a, b);
return 0;
}