I’ve successfully solved Caesar and Vigenere pset but made and used an array that I stumbled upon through trial and error but don’t exactly know why and how it worked. I’m hoping someone could clarify it to me please. I’m actually very unhappy because I don’t know how and why it worked. Initially, and before stumbling into the end result of the array that I made and that worked perfectly for the solution and that passed all the checks, I used the basics I learned to make one. To make an array simply declare an array [ ], give it a name v[ ], and finally give it an integer size of the number of chars that will make up the string in it...i reviewed my notes and it confirms this, am I missing something? On the basis of what I have just mentioned I went ahead and did exactly that for the purpose of “storing” each ciphertext char in it, including spaces and punctuations, then after the whole ciphering process is done to print it out...to my surprise though my initial attempt, keeping in mind the basics of making an array, didn’t work or function as planned and got an error that pointed me in the direction of trying increasing the size of the array simply through dumb interpretation of the error; so I did just that v[strlen + 1] initially. That didn’t work and I got frustrated with curiosity on why that wouldn’t work as it should, so I added +100,000 to the strlen in array v and it worked like a charm! However now I’m really upset as I don’t know why it worked and also that I couldn’t figure out a better way to get the same result as probably everyone else did.
Here’s part of my code that I find relevant (not sure if I should reveal everything here, please tell me to remove it if I shouldn’t):
//If all char in argv[1] are alpabetic chars (function above), get the plaintext from the user string s = get_string("Plaintext: "); int g = strlen(s); char v[g + 8000000]; for (int i = 0, j = 0; i < g; i++) { //To address the chars in plain text that are alphabetic if (isalpha(s[i])) { //Converts string 'filtered from previous function' to an actual integer and MOD to wrap around array of argv[1] once all key chars are exhausted. key = shift(argv[1][j % n]); if (isupper(s[i])) { v[i] = ((s[i] - 'A' + key) % 26) + 'A'; } if (islower(s[i])) { v[i] = ((s[i] - 'a' + key) % 26) + 'a'; } //To go to the next letter in argv[1] j++; } //To not increment j (skip ciphering anything that is not alphabetic) else //To address the chars that are not Alphabetic { if (isspace(s[i])) { v[i] = ' '; } if (ispunct(s[i])) { v[i] = s[i]; } } } //Prints output printf("ciphertext: %s\n", v); } }
I really hope my inquiry is considered legit to ask here and to hopefully get a helpful explanation of why it is working like it is and why my initial attempt (I.e. without increasing the size of array v) did not work?