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

自定义C字符串拼接函数的两次调用及空格添加实现问题

自定义C字符串拼接函数的两次调用及空格添加实现问题

Hey there! Let's work through this step by step—you're really close, just a couple of missteps in the order of operations and missing the space concatenation part. Let's break it down:

First, let's look at the key issues in your current code:

  • You're calling myStrcat(s1, s2) before the user even enters the second string! At that point, s2 is uninitialized (or holds empty garbage), so that first call is either doing nothing useful or causing weird unexpected behavior.
  • You never added the required blank space between the two input strings. To fix this, you can create a simple single-character string (just a space) and pass that to myStrcat after the first input, before appending the second string.

Here's the fixed version of your code with clear explanations:

#include <iostream>
#include <cstring>

using namespace std;

void myStrcat(char s1[], char s2[]);

int main(){
    
    char s1[100];
    char s2[100];
    // Create a small string containing just a single space for concatenation
    char space[] = " ";
    
    cout << "Enter first string: ";
    cin.getline(s1, 100);
    
    // Step 1: Append the blank space to the end of the first string
    myStrcat(s1, space);
    
    cout << "Enter second string: ";
    cin.getline(s2, 100);
    
    // Step 2: Append the second input string to the modified first string
    myStrcat(s1, s2);

    cout << s1 << endl;
    
    return 0;
}

// Parameters:
//  s1: The target string that will be modified (original first input + space + second input)
//  s2: The string to append to the end of s1
// Pre conditions: s1 has enough allocated space to hold the combined result (we used 100 chars which is safe here)
// Post conditions: s1 is updated to be the concatenation of its original value plus s2
void myStrcat(char s1[], char s2[])
{
    int i, j;
    // Move index i to the position of s1's null terminator (the end of the string)
    for (i = 0; s1[i] != '\0'; ++i);
    // Append each character of s2 to s1, incrementing both indices
    for (j = 0; s2[j] != '\0'; ++j, ++i){
        s1[i] = s2[j];
    }
    // Add a new null terminator to mark the end of the combined string
    s1[i] = '\0';
}

Let's walk through the key fixes and changes:

  1. Added a space variable that's just a single space character wrapped in a valid C-string (with a hidden null terminator).
  2. Reordered the myStrcat calls: first append the space right after getting the first input, then append the second string after the user enters it.
  3. Removed the useless early myStrcat(s1, s2) call that ran before the second string was even input.
  4. Cleaned up the function comments for clarity (fixed a tiny typo "srings" to "strings" too!).

When you test this code, it will work exactly as your assignment requires:

  • Ask for the first string (e.g., "Hello") → appends a space to make "Hello "
  • Ask for the second string (e.g., "World") → appends it to make "Hello World"
  • Print the final combined string correctly!

Just a quick note: we're relying on the 100-character array size to avoid overflow here, which is totally acceptable for your assignment's requirements. In production code, you'd want to add checks for available space, but that's beyond the scope of this task.

备注:内容来源于stack exchange,提问作者Marie B.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.15 09:30:28