如何将二维数组赋值给另一个二维数组?

5
组件
我有一个字符串,例如
char block[4][256] = "";
我有一句话
char sentence[256] = "Bob walked his dog";
我还有一个迭代器变量
int pos = 0;
我的目标是
我试图将句子数组sentence中的每个单词按顺序分配到2-d block数组block中。
例如,
假设我有这段代码(我自己写的 - 没有按计划工作)
for (int x=0; x < ((int)strlen(sentence)); x++)
{
    if (sentence[x] == ' ') // not using strcmp at the moment to be more clear
    {
        ++pos; // move to the next word after space
        for (int y=0; y < pos; y++) // scan through the word
        {
            for (int z=0; z < x; z++) // add the word to the array
            {
                block[y][z] = sentence[z]; // assign the block (!!confusion here!!)
            }
        }
    }
}

我的看法

在处理这个问题时,我需要首先扫描句子,直到遇到一个空格 ' ' 字符。在遇到这个字符后,我需要重新扫描句子并开始将所有字符添加到该空格 ' ' 位置之前的第一段块数组 block[y][z] 中,其中 z 是上面 for 语句中的迭代器,y 是每个遇到的空格位置 +1。我认为我主要的问题是理解如何分配二维数组。如果有人看到更好的方法来解决这个问题,我会很高兴听到,谢谢!

我想要的输出

在打印block[x][256] 的内容之后,我希望每个 x 输出我正在扫描的数组中的每个单词。例如,如果我有这样的东西。

for (int a=0; a < 4; a++)
{
    for (int b=0; b < strlen(block[a][]); b++)
    {
        printf("%s\n", block[a][b]);
    }
}

我希望输出为:
block[0][]: Bob 
block[1][]: walked 
block[2][]: his
block[3][]: dog

有人可以帮忙我如何解决这个问题吗?谢谢!

在这方面,很抱歉,我删除了那段代码,因为它是我项目早期的一部分。 - user3251225
如果你在使用C++,为什么不使用std::stringstd::vector<std::string>以及cout等呢?此外,std::istringstream可以用来将输入分隔成单词。换句话说,你的问题可以简化为几行C++代码。 - PaulMcKenzie
1
@user3251225 另外,如果输入包含5个单词呢?10个单词呢?100个单词呢?你将如何调整该数组以容纳超过4个单词的数量?很难相信你的要求总是只有4个单词。 - PaulMcKenzie
C还是C++?在C++中std::vector<std::vector<int>>可以很容易地赋值! - Basile Starynkevitch
我实际上想要一个简单的'C'解决方案。像Oregon提供的那样。在函数调用方面,C++是可以接受的,但我不喜欢它的姿态。Paul和Basile建议的这个std::vector<std::vector<int>>甚至让我更加困惑,因为我不理解这些调用的背景。尽管如此,非常感谢你们两位的努力,但我将选择Oregon的答案! - user3251225
4个回答

4
我认为这就是你想要的。
int word_start = 0, word_end = 0, current_word = 0;

for (int x = 0; x < strlen(sentence) + 1; x++)
{
    if (sentence[x] == ' ' || sentence[x] == '\0')
    {
        word_end = x;
        int y, z;
        for (y = 0, z = word_start; z < word_end; y++, z++)
        {
            block[current_word][y] = sentence[z];
        }
        word_start = x + 1;
        current_word++;
    }
}

这是我用来测试的程序,如果它对你不起作用并且你想看看我如何解释你的问题,请参考以下代码。
#include <stdio.h>
#include <string.h>

int main (const int argc, char * const argv[])
{
    char block[4][256] = {0};
    char sentence[256] = "Bob walked his dog";

    int word_start = 0, word_end = 0, current_word = 0;

    for (int x = 0; x < strlen(sentence) + 1; x++)
    {
        if (sentence[x] == ' ' || sentence[x] == '\0')
        {
            word_end = x;
            int y, z;
            for (y = 0, z = word_start; z < word_end; y++, z++)
            {
                block[current_word][y] = sentence[z];
            }
            word_start = x + 1;
            current_word++;
        }
    }

    for (int x = 0; x < 4; x++)
    {
        printf("%s\n", block[x]);
    }
}

1
为什么不直接使用 memcpy 而不是循环呢? - PaulMcKenzie
4
我认为memcpy是一个优化策略,一旦OP理解了所涉及的原则,他就可以利用它。 - Carey Gregory
我可以展示使用动态分配输出缓冲区和 strlcpy 的正确解决方案,但这样更加直接并符合问题的风格。 - OregonTrail
更新了我的答案。有两个错误。一个是没有检查\0以获取句子中的最后一个单词。另一个是没有使用一个单独的迭代器y将每个单词存储到从索引0开始的block[x]中。 - OregonTrail

4

在存储到块中时,

for (int i=0 ; i < 4 ; i++)
{
    for (int j=0 ; j < 256 ; j++) 
    {
        if (sentence[j] == ' ') 
        {
            block[i][j] = '\0';
            break;
        }
        block[i][j]=sentence[j];
    }
}

在打印时,

for (int i=0 ; i<4 ; i++)
{
    printf ("block[%d][]: %s\n", i, block[i]);
}

感谢您的答复。我很感激您花费时间编写此答案,尽管我正在寻找更普遍的答案,例如Oregons。通过编辑,它不会限制我的四个单词句子。为您的努力加一分。谢谢! - user3251225

3

首先,需要注意的是--如果您需要存储超过4个单词的任何内容,特别是在使用C语言而不是使用C++及其可用的各种容器时,您将遇到更大的问题。

由于答案(迄今为止)都有'C'的解决方案,这里提供一个使用std::istringstreamstd::string的C++解决方案:

#include <sstream>
#include <string>
#include <cstdlib>

int main()
{
    char block[4][256] = { 0 };
    char sentence[] = "Bob walked his dog";

    std::istringstream sstrm(sentence);
    int curWord = 0;
    std::string s;
    while (sstrm >> s)
    {
        memcpy(&block[curWord][0], s.c_str(), s.size());
        ++curWord;
    }
}

更像C++的方法是用std::copy(s.begin(), s.end(), &block[curWord][0])替换memcpy - M.M
当然,如果这在“真实代码”中使用,则应检查 curWord < 4s.size() < 256 或等效内容。 - M.M
尽管我正在寻找更加“C”导向的解决方案,但我确实欣赏这种独特的C++视角。我相信它可能会帮助未来寻找类似答案的人。谢谢!+1 - user3251225

0

以下代码块可以正常工作.. 如果句子中有多个空格,您需要注意。

unsigned int x = 0;
size_t length =  strlen(sentence);
unsigned int word=0 ;
while (x < length)
{
        // copy this word to block
        unsigned charactercount = 0;
        char character = sentence[x++] ;
        while (x<=length && ' ' != character )
        {
            block[word][charactercount++] = character;
            character = sentence[x++] ;
        }
        if (' ' == character || 0 == character)
        {
            block[word][charactercount++] = 0;
            word++;
        }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接