如何为彼此分配2-d阵列?

时间:2014-09-03 03:06:26

标签: c++ c arrays

零件

我有一个字符串,例如

char block[4][256] = "";

我有句子

char sentence[256] = "Bob walked his dog";

我还有一个迭代器变量

int pos = 0;

我想要实现的目标

我正在尝试将数组sentence中的每个单词依次分配到2-d块数组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

任何人都可以帮我解决这个问题吗?谢谢!

4 个答案:

答案 0 :(得分: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 :(得分: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]);
}

答案 2 :(得分:3)

首先,只是作为一个注释 - 如果你需要存储超过4个单词的任何内容,你的问题会更大,特别是如果你在C而不是使用{{1和C ++中可用的各种容器。

由于答案(到目前为止)已经有了&#39; C&#39;解决方案,这是一个使用C++std::istringstream

的C ++解决方案
std::string

答案 3 :(得分: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++;
        }
}