This is the code I have:
// Osman Zakir
// 10 13 2016
// Introduction to Computer Science
// Problem Set 2, caesar.c
// Take the key for the cipher from the user as a command-line argument and use it to encrypt a message
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc == 1 || argc > 2)
{
printf("Error: Please input one number to use as key for cipher!\n");
return 1;
}
printf("Enter plaintext to be shifted by %d positions\n", atoi(argv[1]));
string plaintext = GetString();
int key = atoi(argv[1]);
for (int i = 0, n = strlen(plaintext); i < n; i++)
{
char ciphertext[n];
if (isalpha(plaintext[i]))
{
ciphertext[i] = (plaintext[i] + key) % 26;
}
else
{
printf("%c", plaintext[i]);
}
printf("%c", ciphertext[i]);
}
printf("\n");
return 0;
}
And I need to know what I'm doing wrong. The letters get turned into spaces or new lines for some reason, and only the non-alphabetic characters get printed (since I'm of course having it print them as they are without changing them).