My code for Vigenere is compiling but when I try to print out the Vigenere cipher for a word, the print out is alot longer then the word i prompted. I guess I am doing something wrong with my formula.
cipher[i] = ((current - 'a') + (keyword[j] - 'a') % kwlength)) % 26 + 'a';
kwlength = keywordlength
I think the problem is in the keyword formula:
(keyword[j] - 'a') % kwlength
For the upper case letters I use the same formula accept i am using 'A' instead of 'a'. I don't know how to fix it. Anyone got an idea about what I am doing wrong.
Description of my code:
A for loop which is starting with i = 0 and working till i is the length of my message.
Inside this for loop another for loop with j = 0 and it is checking the length of the keyword.
- An if statement which is checking if the array of chars from the keyword are all alphabetics (with isalpha).
- Then I used another if statement for to check if the message is containing alphabetics. Within this if statement I am checking for lower case and upper case alphabetics.
if (isalpha(message[i]))
{
// Checking lower case alphabetics.
if (islower(message[i]))
{
cipher[i] = ((message[i] - 'a') + (keyword[j % kwlength] - 'a')) % 26 + 'a';
printf("%c", cipher[i]);
}
// Checking upper case alphabetics.
else if (isupper(message[i]))
{
cipher[i] = ((message[i] - 'A') + (keyword[j % kwlength] - 'A')) % 26 + 'A';
printf("%c", cipher[i]);
}
}