My code is not elegant, but even though it's ugly, conceptually, I thought it would work. Nope. I feel as though it's staring me in the face and I can't see it.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main (int argc, string argv[])
{
// accepts one, non-negative command-line argument
if (argc != 2)
{
printf("Usage: ./vigenere k\n");
return 1;
}
// creates variable for keyword
string keyword = argv[1];
// iterates thru keyword and quits if non-alphabetic
for (int i = 0, n = strlen(keyword); i < n; i++)
{
if(!isalpha(keyword[i]))
{
printf("Usage: ./vigenere k\n");
return 1;
}
}
// prompts user for text to encrypt
printf("plaintext:");
string plain = get_string();
// checks user entered valid string
if (plain != NULL)
{
printf("ciphertext:");
// iterates thru plain one char at a time
for (int i = 0, j = 0, pl = strlen(plain), kl = strlen(keyword); i < pl; i++)
{
// checks if char in string is alphabetic letter
if (isalpha(plain[i]))
{
// checks if plaintext char is uppercase
if (isupper(plain[i]))
{
// changes plain from ascii to alphabetic, adds keyword[j], changes back
char cipher_up = (((plain[i] - 'A') + ((toupper(keyword[j]) - 'A') % kl)) % 26) + 'A';
printf("%c", cipher_up);
}
// checks if plaintext char is lowercase
else if (islower(plain[i]))
{
// changes plain from ascii to alphabetic, adds keyword[j], changes back
char cipher_low = (((plain[i] - 'a') + ((toupper(keyword[j]) - 'a') % kl)) % 26) + 'a';
printf("%c", cipher_low);
}
j++;
}
else
{
printf("%c", plain[i]);
}
}
}
printf("\n");
return 0;
}
Case and non-alphabetic characters appear to be handled correctly, so then it's a modulo issue? Thanks for any help. I'm going to try and simplify it by creating addt'l variables.
~/workspace/ $ cd ~/workspace/pset2/vigenere
~/workspace/pset2/vigenere/ $ make vigenere3
clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow vigenere3.c -lcrypt -lcs50 -lm -o vigenere3
~/workspace/pset2/vigenere/ $ ./vigenere3 ABC
plaintext:HELLO HELLO HELLO
ciphertext:HFNJO HENMP HENLO
~/workspace/pset2/vigenere/ $ ./vigenere3 bacon
plaintext:Meet me at the park at eleven am
ciphertext:Nceq ic _r rhe m_pg ]r chdsdn aj
~/workspace/pset2/vigenere/ $