0

所以我有一个练习,我必须连接 2 个字符数组,如下所示:

const int MAXI=100;
char group[MAXI+7]="things/"
char input[MAXI];

cin >> input;
//Doing something here!
cout << group << endl;

必须让 smthing 发生,所以它返回 -- things/input_text --

棘手的部分是我不允许使用指针、字符串库或任何类型的动态数组。

该怎么办?

编辑:我不需要打印它,我需要变量具有值:things/input_text,因为我将用于其他东西!

EDIT2:我不能使用 < string > 库,这意味着我不能使用 strcat() 或该库上的任何东西。我提供了另一个模块,其触发方式如下:

void thing(group, stuff, more_stuff);

就是这样。

4

2 回答 2

3

像这样的东西?

#include <iostream>

using namespace std;
const int MAXI=100;

int main()
{
    char group[MAXI+7]="things/";
    char input[MAXI];
    cin >> input;
    for(int i=0; i<MAXI; i++)
    {
        group[7+i]=input[i];
        if(input[i]=='\0') break;//if the string is shorter than MAXI
    }
    cout << group << endl;
}
于 2013-04-05T18:19:43.893 回答
1
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    const int MAXI=100;
    char group[MAXI+7]="things/";
    char input[MAXI];

    // Warning: this could potentially overflow the buffer
    // if the user types a string longer than MAXI - 1
    // characters
    cin >> input;

    // Be careful - group only contains MAXI plus 7 characters
    // and 7 of those are already taken. Which means we can only
    // write up to MAXI-1 characters, including the NULL terminator.
    for(int i = 0; (i < MAXI - 1) && (input[i] != 0); i++)
    {
        group[i + 7] = input[i];
        group[i + 8] = 0; // ensure null termination, always
    }

    cout << group << endl;

    return 0;
}
于 2013-04-05T18:23:37.513 回答