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.

3 Answers 3

1

Why does it know to do this? Because in C and C++, elements of an array are laid out contiguously in memory. Multidimensional arrays are simply arrays of arrays, and so are also contiguous. So when you write past the first row of your array, it simply keeps writing into memory and happens to get to the next row, since that next row is next up in the memory.

What you can do is the following, if you want to do bounds checking:

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

Or, as HelloMyNameIsRay said, since we already know that new_keyword.length() + new_alphabet.length() == 25, we can simply take out the bounds checking, like so:

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];
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, these work just great! I do have a couple (probably noobish) questions though- 1. (&arr[0][0])[i] and (&arr[0][0])[new_keyword.length() + i] refer to the starting locations for where these two strings are added in, correct? Could you explain to me the purpose of the "&"? I thought arrays were always passed-by-ref by default. 2. What is the purpose of i < ROWCOL and i+new_keyword.length() < ROWCOL? Idk if this is a matter of robustness but, provided that new_alphabet + new_keyword = 25, isn't it redundant?
@HelloMyNameIsRay To answer your second question (new_alphabet + new_keyword == 25), I was dumb and did not realize that. I will fix this in the answer.
@HelloMyNameIsRay The & operator is the address-of operator. So I am taking the address of the first element of the 2d array with @arr[0][0], and then since that is a pointer, I am then acting as if it is a 1d array and am writing to consecutive slots of memory.
0

Jashaszun answeard you correctly about the first part.

About the seconde part, try this:

int j = 0;
for (int i = strlen (new_keyword); i < 25; i++) {
    arr[0][i] = new_alphabet[j];
    j++;
}

1 Comment

Thanks, this more closely follows the format for the for-loop I had. I guess it just starts counting where the previous one left off.
0

As stated earlier, in mutli dim array, the memory is contiguous, just make sure you access the start of the array correctly to point to 'char *' and assign the value there accordingly. I tried this code snipet of for loop and it worked for me. Please check it out, hope this helps.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.