I am not sure why the loop iterating over the keyword (j) is not taking the spaces as as when the plain text is taking §Ca someone help me finding out the error.
so if I take an input as ./vigenere bacon
plaintext: Meet me at the park at eleven am
ciphertext: Negh zf gb nal vcld ht mfxcep oz
the expected ouput should be
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/*
Usage: ./crack hash
single command-line argument: a keyword, k, composed entirely of alphabetical characters.
preserve case
advance to the next leter in the keyword only if it isalpha
*/
int main(int argc, string argv[])
{
// ensure proper usage
if (argc != 2)
{
printf("Usage: ./vigenere keyword\n");
return 1;//error
}
//prompt the user to enter the text to be encrypted
printf("plaintext: ");
string text = get_string();
string key = argv[1];
int len=strlen(key);
int index_U=0,index_l=0,j=0;
char c;
printf("ciphertext: ");
for(int p=0,n=strlen(text); p<n; p++){
//printf("key :%c\n",key[j]);
//ciphertext, c, is computed as:[c = (AI(P) + AI(K)) \bmod 26\] AI-Alphabtical index wherein \(\bmod 26\) here means "remainder when dividing by 26.
index_U = text[p]-65; // alphabetical index of uppercase letter computed from ascii value of charachters
index_l= text[p]-97; // alphabetical index of lowercase letter
int key_U = key[j%len]-65; //alpahetical index of key entered
int key_l =key[j%len]-97;
if(isalpha(text[p])){
if(isupper(text[p]))
{
//converting alphabetical index to ascii value of charachters to print the ciphered letter
if(isupper(key[j]))
{
c = (((index_U + key_U )%26)+65);
}
else
{
c = (((index_U + key_l)%26)+65);
}
}
else if (islower(text[p])){
if(isupper(key[j]))
{
c = (((index_l + key_U)%26)+97);
}
else
{
c =(((index_l + key_l)%26)+97);
}
}
j++;
}
//non-alphabetical characters should be outputted unchanged
else
{
c= text[p];
}
printf("%c",c);
}
printf("\n");
return 0;//signifies success
}