30

问答题 30 /85

编写一个程序实现功能:将两个字符串合并为一个字符串并且输出,用指针实现。
char str1[20]={“Hello ”}, str2[20]={“World ”};

参考答案

#include <stdio.h>

int main()
{
   char str1[20]={“Hello ”}, str2[20]={“World ”};
   char *p=str1, *q=str2;

  while( *p ) 
       p++;
  while( *q )
  {
      *p = *q;
      p++;
      q++;
  }

  *p = ‘\0’;
  printf(“%s\n”, str1);

  return 0;
}