I have seen related queries but that did not help https://gist.github.com/arshsherdill/174fe2945f3d43f3b33e1c307f7fde48
On running as ./vignere bacon. it shows segmentation fault i am unable to find what causes this error. Even debugging did not helped
#include<stdio.h>
#include<cs50.h>
#include<ctype.h>
#include<string.h>
int key_len;
//function to check that all characters in argv[1] are valid characters
int check(string c)
{
key_len=strlen(c);
for(int i=0;i<key_len;i++)
{
if(!isalpha(c[i]))
return 1;
}
return 0;
}
//funvtion to change characters as done in caesar cipher
char caesar(char c,int k)
{
if(isupper(c))
return ((((c-'A')+k)%26)+'A');
if(islower(c))
return ((((c-'a')+k)%26)+'a');
else
return 0;
}
int main(int argc, string argv[])
{
string k="";
if(argc==2)
{
if(check(argv[1])==1)
return 1;
for(int j=0;j<key_len;j++)
{
//I have stored values of key to be used later
k[j] = tolower(argv[1][j])-'a';
}
//plaintext
string p = GetString();
int len_text = strlen(p);
//ciphertext
string c="";
int l=0;//key index
for(int r=0;r<len_text;r++)
{
if(isalpha(p[r])){
//caesar shift function
c[r]=caesar(p[r],k[l]);
//key is used only when the p[r] is a character
if(l<key_len-1){
l++;
}
else
l=0;
}
else
{
c[r]=p[r];
}
}
printf("%s\n",c);
}
//if conditions are not met
else
return 1;
}