指向字符串的两个字符串,要实现连接,分两步完成:
(1)扫描第一个字符串
(2)接第一步继续扫描第二个字符串。注意在最后要添加结束符'\0'。
#include "stdio.h"
#include "conio.h"
int main()
{
char *p1,*p2;
char a[20]="I am" ;
char b[20]=" studen";
p1 =a;
p2 =b;
printf("str1 is :%s\n",p1);
printf("str2 is :%s\n",p2);
while(*p1!='\0') //先将p1指针移至最后一个元素
p1++;
while(*p2!='\0') //p2指向的第一个字符覆盖p1的'\0'
*p1++=*p2++;
*p1='\0';
printf("connect two string is :\n");
printf("%s\n",a);
getchar();
return 0;
}