You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写程序:用gets和puts实现两字符串前半段合并并输出

Solution for Combining First Halves of Two Strings

Got it, let's put together a C program that does exactly what you need. We'll use gets() for input and puts() for output as requested, and walk through how it works step by step.

Step-by-Step Breakdown

  • First, declare three string buffers: CH1, CH2, and CH3. Make sure CH3 is large enough to hold the combined first halves of the other two strings.
  • Use gets() to read input strings into CH1 and CH2. Quick note: While gets() is required here, it's important to mention that gets() is unsafe in modern C because it doesn't check buffer boundaries—for real-world code, use fgets() instead.
  • Calculate the length of each string with strlen() to find the halfway point. For a string of length n, the first half will be the first n/2 characters (integer division handles both even and odd lengths naturally).
  • Copy the first half of CH1 into CH3, then append the first half of CH2 to CH3. Don't forget to add a null terminator ('\0') to CH3 to make it a valid C string.
  • Finally, use puts() to print the resulting CH3.

Complete Program Code

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

int main() {
    // Define buffers for input strings and the result
    char CH1[100], CH2[100], CH3[200];
    int len1, len2, current_index = 0;

    // Read input strings using gets()
    printf("Enter first string (CH1): ");
    gets(CH1);
    printf("Enter second string (CH2): ");
    gets(CH2);

    // Calculate lengths of the input strings
    len1 = strlen(CH1);
    len2 = strlen(CH2);

    // Copy first half of CH1 to CH3
    for (int i = 0; i < len1 / 2; i++) {
        CH3[current_index++] = CH1[i];
    }

    // Append first half of CH2 to CH3
    for (int j = 0; j < len2 / 2; j++) {
        CH3[current_index++] = CH2[j];
    }

    // Add null terminator to make CH3 a valid string
    CH3[current_index] = '\0';

    // Print the result using puts()
    printf("\nResulting string (CH3): ");
    puts(CH3);

    return 0;
}

Example Output

Enter first string (CH1): HelloWorld
Enter second string (CH2): StackOverflow

Resulting string (CH3): HelloStack

(Explanation: "HelloWorld" is 10 characters, so its first half is the first 5: "Hello". "StackOverflow" is 13 characters, so its first half is the first 6: "Stack". Combining them gives "HelloStack".)

Alternative Concise Approach

Instead of manual loops, you can use strncpy() for cleaner code:

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

int main() {
    char CH1[100], CH2[100], CH3[200];
    int len1, len2;

    printf("Enter first string (CH1): ");
    gets(CH1);
    printf("Enter second string (CH2): ");
    gets(CH2);

    len1 = strlen(CH1);
    len2 = strlen(CH2);

    // Copy first half of CH1
    strncpy(CH3, CH1, len1 / 2);
    // Append first half of CH2
    strncpy(CH3 + len1 / 2, CH2, len2 / 2);
    // Add null terminator
    CH3[len1/2 + len2/2] = '\0';

    printf("\nResulting string (CH3): ");
    puts(CH3);

    return 0;
}

内容的提问来源于stack exchange,提问作者rtg2.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:57:03