Can someone help me with my code to solve this error above?I have searched for answers but they are bit different from my code.
- vigenere.c compiles.
- encrypts "a" as "a" using "a" as keyword
- encrypts "barfoo" as "caqgon" using "baz" as keyword
- encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
failed expected "ciphertext: CaQ...", not "ciphertext: CaQ..." - encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
- encrypts "world!$?" as "xoqmd!$?" using "baz" as keyword
- encrypts "hello, world!" as "iekmo, vprke!" using "baz" as keyword
- handles lack of argv[1]
failed failed to execute program due to segmentation fault - handles argc > 2
- rejects "Hax0r2" as keyword
#include<stdio.h>
#include<cs50.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int main(int argc,string argv[])
{
for (int k = 0;k<strlen(argv[1]);k++)
{
if (isalpha(argv[1][k]))
{}
else
{
return EXIT_FAILURE;
}
}
if (argc == 2)
{
int alpha,cipher,key,j=0,len=strlen(argv[1]);
char a;
string plaintext = get_string("Text: ");
printf("ciphertext: ");
for (int i = 0; i<strlen(plaintext) ; i++)
{
if (isalpha(plaintext[i]))
{
if (isupper(plaintext[i]))
{
alpha = plaintext[i]-65;
key = argv[1][j]-65;
cipher = (key+alpha)%26;
a = (char) cipher+65;
printf("%c",a);
j=(j+1)%len;
}
else if (islower(plaintext[i]))
{
alpha = plaintext[i]-97;
key = argv[1][j]-97;
cipher = (key+alpha)%26;
a = (char) cipher+97;
printf("%c",a);
j=(j+1)%len;
}
}
else
{
printf("%c",plaintext[i]);
}
}
printf("\n");
}
else
{
printf("error\n");
return EXIT_FAILURE;
}
}