*UPDATE: Actually, I can't understand a thing now. Let's take an example from check50. Our program should encrypt "world, say hello!" with a keyword "baz". It expects "xoqmd, rby gflkp!" as a correct encryption. But, wait a second! If I do it manually on paper, I will get:
world, say hello!
bazba--zba-zbazb-
which means I should move "w" by 2 (the number of "b" in the alphabet), "o" by 1 (the number of "a" in the alphabet) and "r" by 26 (the number of "z" in the alphabet) etc. I should not, however pay any attention to coma, exclamation point and space, right? So on paper I will get an encrypted message of "yprne, scz hdmlq!" which somehow does not equal the supposedly correct answer of "xoqmd, rby gflkp!" Why? Also, my program gives me yet another variant, encrypting it as a "yprle, tay hemlo!" Why? Please help me understand all this.
Can you please point me to the mistakes? But if it is possible, don't just correct the code, give me a hint of what to think about.
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main (int argc, string argv[])
{
// checking, if there is indeed 2 command-line argumets
if (argc != 2)
{
printf("Don't joke with me, cowboy! I said enter the name of the programm AND one keyword ONLY. Try again!");
return 1;
}
// making a string out of the 2nd command-line argument
string keyword = argv[1];
// checking, if the keyword given is alphabetcial only
for (int i = 0, l = strlen(keyword); i < l; i++)
{
if (isalpha(keyword[i]))
{
}
else
{
printf("Looks like you've entered an invalid keyword. Remeber, your keyword must contain letter only. Try again!");
return 1;
}
}
// getting the text the user wants to cipher
printf("Enter the text you want to cipher: \n");
string plaintext = GetString();
// storing the length of the keyword
int kwl = strlen(keyword);
// a loop, that will go through the plaintext char by char, ciphering it with a corresponding keyword value
for (int i = 0, j = 0, l = strlen(plaintext); i < l; i++, j++)
{
// Firstly - checking, if the programm should wrap over to the beginning of the key word, if its length was reached. Secondly - checking, if the current char in the keyword is upper- or lowercase, and
if (j > kwl)
{
j = j%kwl;
}
int k = keyword[j];
int kalph;
if (isupper(keyword[j]))
{
kalph = k - 64;
}
else if (islower(keyword[j]))
{
kalph = k - 96;
}
// once the programm knows, by what value it should shift the original char, it checks, whether the current original char is upper- or lower case, and shifts it.
if (isalpha(plaintext[i]) && plaintext[i]!= ' ' && isupper(plaintext[i]))
{
int shiftalph = (plaintext[i] - 64) + kalph;
if (shiftalph > 25)
{
shiftalph = shiftalph % 26;
}
int ascii = shiftalph + 64;
printf("%c", ascii);
}
else if (isalpha(plaintext[i]) && plaintext[i]!= ' ' && islower(plaintext[i]))
{
int shiftalph = (plaintext[i] - 96) + kalph;
if (shiftalph > 25)
{
shiftalph = shiftalph % 26;
}
int ascii = shiftalph + 96;
printf("%c", ascii);
}
else
printf("%c", plaintext[i]);
}
}
if (isalpha(keyword[i]))
the body is empty, so you might as well typeif (!isalpha(keyword[i]))
which essentially skips right to theelse
part.