Good day everyone. I am trying to do a Vigenere cipher code in C and I have done something that is wrong and I can't fix it. I have documented my code as best as I can to make you know my intentions
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, string argv[])
{
if (argc == 2)
{
string keyword = argv[1];//step 1: Get keyword from commandline argument
for (int i = 0, n = strlen(keyword); i < n; ++i )//step 2: Keyword must be alphabetic
{
if (!isalpha(keyword[i]))
{
printf("second argument should be a string of alphabets \n");
return 1;
}
}
printf("plaintext: "); //step 3: Prompt for plaintext
string p = get_string();
printf("ciphertext: ");
if( p != NULL)
{
for (int i = 0, n = strlen(p); i < n; ++i) // Go through every character in the plaintext
{
int indexOfKeyword = keyword[i % strlen(keyword)];
if (isalpha(p[i])) //if a character in the plaintext is an alphabet
{
if (isupper(p[i])) //if character in the plaintext is a capital letter
{
keyword[indexOfKeyword] = keyword[indexOfKeyword] - 'A';
p[i] = p[i] - 'A'; // preserve the plaintext's case by converting to its alphabet position using this formula
//use the alphabet position in the formula to get the new alphabet position and convert back to ascii
printf("%c",( (p[i] + keyword[indexOfKeyword + 1 % strlen(keyword)]) % 26 ) + 'A');
}
if (islower(p[i]))
{
keyword[indexOfKeyword] = keyword[indexOfKeyword] - 'a';
p[i] = p[i] - 'a'; // preserve the plaintext's case by converting to its alphabet position using this formula
//c[i] = ((p[i] + keyword[i]) % 26) + 'a'; //use the alphabet position in the formula to get the new alphabet position and convert back to ascii
printf("%c",( (p[i] + keyword[indexOfKeyword + 1 % strlen(keyword)]) % 26 ) + 'a');
}
}
else
{
//c[i] = p[i];//don't shift plaintext character if it is a symbol just put it in c
printf("%c",p[i]);
}
}
}
printf("\n");
return 0;
}
else
{
printf("Program should have 2 arguments!\n");
return 1;
}
return 0;
}