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,
- accept a keyword
- remove all redundant letters from said keyword
- add this new keyword into a 5x5 array, char by char.
- 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.