My code compiles and I tested it with the example "Meet me at the park at eleven am", the result was "Negh zf av huf pcfx bt gzrwep oz", and that is correct. But when I run "check50 cs50/2018/x/vigenere" command I get different results... My code is little bit messy right now, I apologize for that.
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argv[1] != 0)
{
// Getting the command line argument to use it like a key
string key = argv[1];
// Count the entered no letter symbols
int count_non_letters = 0;
for (int i = 0; i < strlen(key); i++)
{
if (key[i] >= 48 && key[i] <= 57)
{
count_non_letters++;
}
}
// Checking if the user inputs command line argument, if so, the program runs
if ((argc < 3) && (count_non_letters == 0))
{
// The index number of the keytext array
int key_index;
// The index is based on the length of the alphabet
char alpha_index[26];
// Checking if the entered symbol is alphabet
bool is_alpha = false;
// If the plain text is larger than the crypt text we loop over and over again the letters in the crypt text to match the plain text size
int crypt_text_loop;
// The index number of the array of string
int index;
// Simple count to help for the array filling
int count = 0;
// The formula to cript the text
int crypt = 0;
// Counting the alphabetical symbols in the plain text
int count_alpha = 0;
// Asking the user to enter the plain text
string plain_text = get_string("Enter text: ");
for (int i = 'A'; i <= 'Z'; i++)
{
alpha_index[count] = i;
count++;
}
// Checking the entered text and encrypting it
printf("ciphertext: ");
// Checking every symbol of the entered text
for (int i = 0; i < strlen(plain_text); i++)
{
// Checking if the character is a letter
if ((plain_text[i] >= 65 && plain_text[i] <= 90) || (plain_text[i] >= 97 && plain_text[i] <= 122))
{
is_alpha = true;
index = toupper(plain_text[i]) - alpha_index[0];
crypt_text_loop = alpha_index[count_alpha] % strlen(key);
key_index = toupper(key[crypt_text_loop]) - alpha_index[0];
crypt = (index + key_index) % 26;
count_alpha++;
// Checking if the character is a capital letter
if (isupper(plain_text[i]) && is_alpha)
{
printf("%c", alpha_index[crypt]);
}
// Checking if the character is a small letter
if (islower(plain_text[i]) && is_alpha)
{
printf("%c", tolower(alpha_index[crypt]));
}
}
// If is not a letter, then is a symbol or space and we print it like it is
else
{
printf("%c", plain_text[i]);
}
}
printf("\n");
}
else
{
return 1;
}
}
else
{
return 1;
}
}