0
// Receive plaintext
string text = get_string("plaintext: ");
int t = strlen(text);

char ciphertext[t];

//Array assignment
for (int i = 0; i < t; i++)
{
    // Plaintext to Ciphertext
    ciphertext[i] = cipher(text[i], key);
}

printf("ciphertext: %s\n", ciphertext);

That is my code where the plaintext is gotten from the human

Cipher is basically a function that takes the plaintext character (text[i]) and converts it to the ciphertext character.

I noticed that the first couple of tries of executing the code after I clear my terminal window usually come out okay, but subsequently, there are extra characters. What should i do?

Thanks in advance.

3
  • 1
    Best guess without seeing the code: ciphertext is not properly null terminated. It is declared a char array, printf uses %s (string) format; the thing that makes a string a string is the null-terminator. Apr 18 '20 at 12:09
  • Dino, redo this as an answer.
    – Cliff B
    Apr 19 '20 at 3:36
  • Thank you @dinocodersaurus. I declared ciphertext as a null string and it works well now. Thank you so much
    – Kalahari
    Apr 19 '20 at 17:15

Browse other questions tagged .