I've been struggling with Vigenere for a while now, and now that I "believe" I got to the point, I get a segmentation fault error when I run the program. Weird thing is that while I try to spot the issue with Debug50, the tool doesn't return this error and actually allows me to run the program until the end... but it still gives a wrong result ^^
Would you be able to spot the bug that causes the segmentation fault ?
Thanks a lot!
Here's my code :
int main(int argc, string argv[])
{
if (argc == 2 && isalpha(argv[1]))
{
char *k = argv[1];
string p = get_string("plaintext: ");
for (int i = 0, n = strlen(p); i < n; i++)
{
for (int j = 0, o = strlen(k); j < o; j++)
{
if (isupper(p[i]))
{
printf("%c",(((int)p[i] + (k[j] % o)) - 65) % 26 + 'A');
}
else if (islower(p[i]))
{
printf("%c",(((int)p[i] + (k[j] % o)) - 97) % 26 + 'a');
}
else
{
printf("%c", p[i]);
}
}
}
printf("ciphertext: %s\n", p);
return 0;
}
else
{
printf("Usage: ./vigenere k\n");
return 1;
}
}