Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C++中使用间隔?_C++_Arrays_String_Matrix_Strcat - Fatal编程技术网

如何在C++中使用间隔?

如何在C++中使用间隔?,c++,arrays,string,matrix,strcat,C++,Arrays,String,Matrix,Strcat,这是我的代码: #include <iostream> #include <string.h> using namespace std; const int MAX_SIZE1 = 20; const int MAX_SIZE2 = 10; int main() { char a[MAX_SIZE1][MAX_SIZE1][MAX_SIZE2]; int n, i, j; cin >> n; for (i = 0; i &l

这是我的代码:

#include <iostream>
#include <string.h>
using namespace std;

const int MAX_SIZE1 = 20;
const int MAX_SIZE2 = 10;

int main()
{
    char a[MAX_SIZE1][MAX_SIZE1][MAX_SIZE2];
    int n, i, j;
    cin >> n;
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            cin >> a[i][j];

    char s[MAX_SIZE1 * MAX_SIZE1 * (MAX_SIZE2 - 1) + 1];
    int hor = 0, vert = 0;
    while (hor < n / 2 && vert < n / 2)
    {
        for (i = vert; i < n - vert; i++)
            strcat(s, a[hor][i]);
        for (i = hor + 1; i < n - hor; i++)
            strcat(s, a[i][n - vert - 1]);
        for (i = n - vert - 2; i >= vert; i--)
            strcat(s, a[n - hor - 1][i]);
        for (i = n - hor - 2; i > hor; i--)
            strcat(s, a[i][vert]);
        hor++;
        vert++;
    }
    if (n % 2)
        for (i = vert; i < n - vert; i++)
            strcat(s, a[hor][i]);
    else
        for (i = hor; i < n - hor; i++)
            strcat(s, a[i][vert]);
    cout << s << endl;
    return 0;
}
我有一些问题。如何修改它以获得输出的s字符串中单词之间的间隔?还有,如何在我的输出开始时去掉50行奇怪的符号

编辑:对不起。我觉得没关系。输入最多应为20x20个字的数组,每个字不超过9个字符。输出应该是一个s字符串,它表示从左上角开始以顺时针螺旋读取数组而形成的句子。问题是。。我在输出开始时得到了奇怪的符号,在单词之间没有间隔。< /P>使用C++代替C。 <>因为你使用C++,你应该用C++的方式编写你的代码,使用STD::String。你的问题之一是未初始化的字符串可以这样固定,因为在C++中不可能定义一个未初始化的字符串。 替换

#include <string.h>
char s[MAX_SIZE1 * MAX_SIZE1 * (MAX_SIZE2 - 1) + 1];
strcat(s, a[hor][i]);

替换

#include <string.h>
char s[MAX_SIZE1 * MAX_SIZE1 * (MAX_SIZE2 - 1) + 1];
strcat(s, a[hor][i]);

如果你还想用C 您必须初始化输出字符串:

char s[MAX_SIZE1 * MAX_SIZE1 * (MAX_SIZE2 - 1) + 1] = "";
未初始化的字符串通常包含垃圾,strcat会添加垃圾,而不是删除垃圾

此外,如果需要分隔符,则应显式地对其进行编码;strcat本身不添加任何分隔符:

strcat(s, ",");

请提供具体的输入和预期的输出。我们不能键入20x20x10次来测试程序。在C++中,您将使用STD::string,而不是C样式的GabbRy。对不起,非常感谢!我还有一个问题。如果你能回答或者至少给我一个提示,我会非常感激的。从左上角开始顺时针读取数组相对容易,但我似乎不知道如何从右下角开始逆时针读取。@user3213110请将此作为单独的问题发布,以避免混淆