1. 程式人生 > >C語言 指標實現字串連線

C語言 指標實現字串連線

C語言 指標實現字串連線

#include <stdio.h>
#include <string.h>

int main()
{
	char s1[100], s2[100], s3[100];
	char *p1=NULL;
	char *p2=NULL;
	char *q=s3;//將字元指標 指向 字元陣列s3
	char *result = q;
	printf("請輸入第一個字串:");
	gets(s1);
	printf("請輸入第二個字串:");
	gets(s2);
	p1=s1;
	p2=s2;
	
	while(*p1 != '\0')
	{
		*q=*p1;
		printf("%c ", *q); //測試 
		p1++;
		q++;
	}
	
	while(*p2!='\0')
	{
		*q=*p2;
		printf("%c ", *q); //測試 
		p2++;
		q++;
	}
	*q='\0';
	//printf("ok"); //測試 
	
	printf("%s", result);
}