Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign 2-d arrays' to one another?

Tags:

c++

arrays

c

Components

I have a string, for example

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

and I have sentence

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

I also have an iterator variable

int pos = 0;

What I am trying to achieve

I am trying to assign each word in the array sentence into the 2-d block array block in sequence.

For instance,

Let's say I have this code (my own write-up - didn't work as I had planned)

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!!)
            }
        }
    }
}

How I see it

The way I see it by approaching this problem I need to first scan the sentence until I encounter a space ' ' character, a blank. After encountering this I have to re-scan the sentence and start adding all the characters up to the position of that blank space ' ' into the first segment of my block array block[y][z], z being the iterator in the for statement above, and y being the position + 1 for every space encountered. I believe my main problem here is understanding how to assign 2-d arrays. If anyone see's a better approach to this problem I would love to hear it, thanks!

The output I want

After printing the contents of block[x][256] I want every x to output each word in the array that I am scanning for example. If I have something like this.

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

I want the output to be:

block[0][]: Bob 
block[1][]: walked 
block[2][]: his
block[3][]: dog

Can anyone help of how I can approach this problem? Thanks!

like image 998
user3251225 Avatar asked Sep 03 '14 03:09

user3251225


3 Answers

I think this is what you were going for.

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++;
    }
}

Here's the program that I used to test it, if it's not working for you and you'd like to see how I interpreted your question.

#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]);
    }
}
like image 143
OregonTrail Avatar answered Oct 23 '22 11:10

OregonTrail


While storing into block,

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];
    }
}

While printing,

for (int i=0 ; i<4 ; i++)
{
    printf ("block[%d][]: %s\n", i, block[i]);
}
like image 4
Adarsh Avatar answered Oct 23 '22 11:10

Adarsh


First, just as a note -- your problem will be much greater if you ever need to store anything above 4 words, and especially if you're writing things in C instead of using C++ and the various containers that are available in C++.

Since the answers (so far) have 'C' solutions, here is a C++ solution using std::istringstream and std::string:

#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;
    }
}
like image 3
PaulMcKenzie Avatar answered Oct 23 '22 09:10

PaulMcKenzie