I'm having issues figuring out how to ignore the spaces in the user string when changing to ciphertext. I thought the isalpha() check accounted for that but I guess not.
Any help would be appreciated! Thanks.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ASCII_TO_ALPHA 65
#define LOWER_ASCII_TO_ALPHA 6
int main(int argc, string argv[])
{
int key_length;
int string_length;
int key_index;
if(argc == 2)
{
string cipher_key = argv[1];
key_length = strlen(cipher_key);
// Check if every character is an alphabet
for(int k = 0; k < key_length; k++)
{
if(!isalpha(cipher_key[k]))
{
printf("Usage ./vingenere OneStringOnlyLetters\n");
return 1;
}
}
string plain_text = get_string("plaintext: ");
string_length = strlen(plain_text);
char cipher_text[strlen(plain_text) + 1];
for (int i = 0; i < string_length; i++)
{
if (isalpha(plain_text[i]))
{
key_index = i % key_length;
if (isupper(plain_text[i]))
{
cipher_text[i] = (char)(((((int) plain_text[i] - ASCII_TO_ALPHA) + (toupper(cipher_key[key_index]) - ASCII_TO_ALPHA)) % 26) + ASCII_TO_ALPHA);
}
else if(islower(plain_text[i]))
{
cipher_text[i] = tolower((char)(((((int) plain_text[i] - LOWER_ASCII_TO_ALPHA - ASCII_TO_ALPHA) + (toupper(cipher_key[key_index]) - ASCII_TO_ALPHA)) % 26) + ASCII_TO_ALPHA));
}
}
else
{
cipher_text[i] = plain_text[i];
}
}
cipher_text[strlen(plain_text)] = '\0';
printf("ciphertext: %s\n", cipher_text);
return 0;
}
else
{
printf("Usage ./vingenere OneStringOnlyLetters\n");
return 1;
}
}