I had my Vigenere cipher up and running without any errors, but the ciphertext wasn't matching up like it was supposed to. I copied just the part of code meant to produce the key into a new program so I could see what it was producing:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
//verify cmd line argument exists
if(argv[1] == NULL)
{
printf("Remember to provide a key word after ./vigenere!\n");
return 1;
}
//make key string from cmd line argument
string key = argv[1];
//verify key is only one word of only letters
for(int i = 0, n = strlen(key); i < n; i++)
{
if(!isalpha(key[i]))
{
printf("The key must use only letters!\n");
return 1;
}
if(argc > 2)
{
printf("The key can only be one word!\n");
return 1;
}
}
//finalize key by subtracting 65 from caps and 97 from lows
for(int i = 0, n = strlen(key); i < n; i++)
{
if(isupper(key[i]))
key[i] = key[i] - 65;
if(islower(key[i]))
key[i] = key[i] - 97;
}
for(int i = 0, n = strlen(key); i < n; i++)
{
printf("%i", key[i]);
}
printf("\n");
}
As I suspected, this is where my problem is -- the key is only one integer. For example, if I input "panda" as my command line argument, the key only results in 15, where it's supposed to result in {15, 0, 13, 3, 0}
What am I doing wrong? How do I make the key an array rather than a single number? I thought string key = argv[1];
would do that, but clearly not. Thanks in advance!