i have been trying so hard to figure out what the shifting problem is in the encrypt function but i have no clue so i would like it if any one could tell me what is happening. I have put a "----here----" marker before the line the problem is happening.
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
//an encrypter function
void encrypt(string plainText, string key);
//check if the key is valid
bool checkKey(string key);
//check if a charater is an int
bool isInt(char value);
int main(int argc, string argv[])
{
// check if there aren't more than one
// command line arguments
if (argc != 2)
{
printf("Usage: ./vigenere keyword\n");
return 1;
}
//check if the key is valid
if (!checkKey(argv[1]))
{
printf("Usage: ./vigenere keyword\n");
return 1;
}
string plainText = get_string("plaintext: ");
//intf("here");
encrypt(plainText, argv[1]);
}
void encrypt(string plainText, string key)
{
printf("ciphertext: ");
int keyIndex = 0;
//iterate through the plaintext and encrypt each
//character then print it to the screen
for (int i = 0; i < strlen(plainText); i++)
{
if (islower(plainText[i]))
{
//----here----
printf("%c", ((int)(plainText[i] + key[keyIndex] ) % (int)'z'));
}
else if (isupper(plainText[i]))
{
//----here----
printf("%c", ((int)(plainText[i] + key[keyIndex] ) % (int)'Z') );
}
else
{
printf("%c", plainText[i]);
}
if (keyIndex<strlen(key))
{
keyIndex++;
}
else
{
keyIndex = 0;
}
}
printf("\n");
}
bool checkKey(string key)
{
//iterate througth the key and check
//if all of its digits are non number characters
//else return flase
for (int i = 0; i < strlen(key); i++)
{
//if the digit is an int
//return false because we want a word not an int
if (isInt(key[i]))
{
return false;
}
}
//return true at last if all the digits
//are numbers
return true;
}
bool isInt(char value)
{
//if the character is between 0 and 9 then
//it is an int
if (value >= '0' && value <= '9')
{
return true;
}
else
{
//if it isnt an int return false
return false;
}
}