In the below code, I get different output depending on where I place the 'Printf("Ciphertext: )' statement, and I cannot understand why. Could anyone please point out what I am missing? Below you can find the code, and the output (variable piece in bold).
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
int j = strlen(argv[1]);
if (argc != 2)
{
printf ("Usage: ./caesar key\n");
return (1);
}
else
{
for (int i = 0; i < j; i++)
{
if (!isdigit (argv[1][i]))
{
printf ("Fail\n");
return(1);
}
}
int convert = atoi (argv[1]) ;
string Plaintext = get_string("Plaintext: ");
int z = strlen(Plaintext);
**printf("Ciphertext: ");**
for (int i = 0; i<z;i++)
{
char Ciphertext [z];
Ciphertext [i] = (Plaintext [i] + 1);
}
for (int i = 0; i<z;i++)
{
char Ciphertext [z];
printf("%c", Ciphertext [i]);
}
printf("\n");
}
}
If I run ./caesar with this code, and enter 'Hey' in the plaintext prompt, the output is Ciphertext: ifz, with which I am happy. However, when I place the printf("Ciphertext: ")Statement to a place that seems more logical to me, I don't get the same output:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
int j = strlen(argv[1]);
if (argc != 2)
{
printf ("Usage: ./caesar key\n");
return (1);
}
else
{
for (int i = 0; i < j; i++)
{
if (!isdigit (argv[1][i]))
{
printf ("Fail\n");
return(1);
}
}
int convert = atoi (argv[1]) ;
string Plaintext = get_string("Plaintext: ");
int z = strlen(Plaintext);
for (int i = 0; i<z;i++)
{
char Ciphertext [z];
Ciphertext [i] = (Plaintext [i] + 1);
}
**printf("Ciphertext: ");**
for (int i = 0; i<z;i++)
{
char Ciphertext [z];
printf("%c", Ciphertext [i]);
}
printf("\n");
}
}
When I do the exact same here, I print Ciphertext: and an empty row, but it doesn't print out the ciphertext [] as above. Any ideas?