我需要使用 get 和 puts 合并一半的字符串

问题描述

编写一个程序,读取两个字符串 CH1 和 CH2,并将 CH1 的前半部分和 CH2 的前半部分复制到第三个 CH3 字符串中。使用 getsputs 函数显示结果。

输出应该是这样的 enter image description here

解决方法

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

int main(void) 
{
    char CH1[20],CH2[20],eCH1[20],eCH2[20] ; // I used e for exit string 
int i,j ;
printf("Enter first string:");
gets(CH1);
printf("Enter second string:");
gets(CH2);
i = strlen(CH1);
j = strlen(CH2);
strncpy(eCH1,CH1,i/2);
strncpy(eCH2,CH2,j/2);
strcat(eCH1,eCH2);
printf("The half of first string + The half of second string :%s",eCH1);

return 0;
}