0

I'm having some trouble figuring out how to add a string, char by char, into a 2-dim array.

As part of making a substitution cipher for encoding "secret messages," I'm trying to,

  1. accept a keyword
  2. remove all redundant letters from said keyword
  3. add this new keyword into a 5x5 array, char by char.
  4. For the remaining spaces in the array, fill them in alphabetically (unique letters only, meaning excluding those already entered from the keyword). Also, since this is a 5x5 array, this also means I'm ignoring the letter Z. I won't bother trying to encode that.

In other words, I'm trying to do something to the effect of:

string keyword = "PREPARATION";
string new_keyword = "PREATION";   // removed redundant letters, only unique ones remain

string alphabet =     "ABCDEFGHIJKLMNOPQRSTUVWXY";
string new_alphabet = "BCDFGHJKLMQSUVWXY";   //notice chars P, R, E, A, T, I, O, N are no longer present

This first part I had no problem figuring out. But then, I want to add new_keyword and new_alphabet into the array.

     0   1   2   3   4
   ---------------------
  0| P | R | E | A | T |
   ---------------------
  1| I | O | N | B | C |
   ---------------------
  2| D | F | G | H | J |
   ---------------------
  3| K | L | Q | Q | S |
   ---------------------
  4| U | V | W | X | Y |
   ---------------------

string new_keyword is added in first, followed by string new_alphabet. So, at this point, my code is something like this:

string new_keyword = "PREATION";
string new_alphabet = "BCDFGHJKLMQSUVWXY";

const int ROW = 5;
const int COL = 5;

char arr[row][col] = {0}; //initialize all values in array to 0

for(int i = 0; i < new_keyword.length(); i++)
{
    arr[0][i] = new_keyword[i];
}

What this does so far is put in new_keyword, char by char, into the 2dim array. Obviously, "PREATION" is a string longer than 5 chars, yet the program "knows" this apparent overlap, and continues to add the remaining characters "ION" into the second row.

arr[1][0] == 'I';
arr[1][1] == 'O';
arr[1][2] == 'N';

Why does it know to do this?

Additionally, I still need to add new_alphabet beginning at arr[1][3], and though I could explicitly code something like "begin adding in new_alphabet @ arr[1][3]", this obviously has to be "dynamic" and begin adding into the element immediately following any new_keyword. How do I go about doing this?

I guess I'm still pretty fuzzy on 2dim arrays, and the book I own doesn't really cover this sort of case, so any and all help is very much appreciated.

4

3 回答 3

1

Why does it know to do this?因为在 C 和 C++ 中,数组的元素在内存中是连续布局的。多维数组只是数组的数组,因此也是连续的。因此,当您写入数组的第一行时,它只是继续写入内存并碰巧到达下一行,因为下一行是内存中的下一行。

如果您想进行边界检查,您可以执行以下操作:

const int ROW = 5;
const int COL = 5;

char arr[ROW][COL] = {0};

for (int i = 0; i < new_keyword.length() && i < ROW * COL; i++)
    (&arr[0][0])[i] = new_keyword[i];

for (int i = 0; i < new_alphabet.length() && i + new_keyword.length() < ROW * COL; i++)
    (&arr[0][0])[new_keyword.length() + i] = new_alphabet[i];

或者,正如 HelloMyNameIsRay 所说,既然我们已经知道了new_keyword.length() + new_alphabet.length() == 25,我们可以简单地取出边界检查,如下所示:

const int ROW = 5;
const int COL = 5;

char arr[ROW][COL] = {0};

for (int i = 0; i < new_keyword.length(); i++)
    (&arr[0][0])[i] = new_keyword[i];

for (int i = 0; i < new_alphabet.length(); i++)
    (&arr[0][0])[new_keyword.length() + i] = new_alphabet[i];
于 2013-07-09T23:15:29.810 回答
0

如前所述,在 mutli dim 数组中,内存是连续的,只需确保您正确访问数组的开头以指向 'char *' 并相应地在那里分配值。我尝试了 for 循环的这段代码片段,它对我有用。请检查一下,希望这会有所帮助。

int len = new_keyword.length();

for ( int i =0; i < 25; i++){
    if ( i < len)
        *(*arr +i) = new_keyword[i];
    else
        *(*arr+i) = new_alphabet[i-len];
}
于 2013-07-10T15:22:26.620 回答
0

Jashaszun 正确地回答了你关于第一部分的问题。

关于第二部分,试试这个:

int j = 0;
for (int i = strlen (new_keyword); i < 25; i++) {
    arr[0][i] = new_alphabet[j];
    j++;
}
于 2013-07-09T23:20:24.087 回答