This is a Vigenere cypher for cs50. It's my first time coding and I'm going around this for a week now, and I don't seem to be able to print first letter after the loop finishes for the first time.
For example:
jharvard@appliance (~/Dropbox): ./viginere abcde You're key is abcde Type your text: aaaaa aaaaaa aaaaaaa aaaaaaaa abcde bcde bcdebc debcde
First a
is printed but then it starts on b
and in the end it doesn't print every letter. The key is chosen by the user.
I have no idea what I'm doing wrong.
for (int i = 0, j = strlen(plain_text), l = 0; i < j; i++)
{
int rotation_1 = (tolower(plain_text[i]) + (key[l] - 97)) % 122;
int rotation_2 = (plain_text[i] + (key[l] - 97)) % 122;
//if it is a letter
if (isalpha(plain_text[i]))
{
l = l % strlen(key);
//if the it is uppercase
if (isupper(plain_text[i]))
{
printf("%c", toupper(rotation_1));
}
//else if it is lowercase
else
{
printf("%c", rotation_2);
}
l++;
}
// if it is not a letter we print it as it is
else
{
printf("%c", plain_text[i]);
}
}