I'm trying to complete the vigenere code assignment but sadly, despite it seemingly working great with lowercase letters, it completely bugs out with capital letters.
This is how it looks like with lowercase:
(command line) - ./vigenerem BAZ
(input) - barfoobarfoobarfoo
(output) - caqgoncaqgoncaqgon
and this is how it looks like with capital letters:
(command line) - ./vigenerem BAZ
(input) - BARFOOBARFOOBARFOO
(output) - CAQG5N)'Q-5N)'Q-5N
It is worth noting that all other tests pass fine, it works in all cases except for this specific one, and I'm unable to see (despite sitting around 3 hours now on this specific bug) what causes the capital letters specifically to bug out completely on me.
After some debugging (I added it as a note (the else - buggio lines) I found out that apparently k[ki] is not considered to be an alphabet letter after running k amount of cycles (meaning, if the key is 3 letters long, on the 4th it'll stop the program and tell me that k[ki] is infact not a letter, weirdly enough.
And that's all I'm able to find out, after deleting this little debugging command, it all works as long as I'm not using capital letters.
Please help, I'm getting quite desperate =/
Here is the full code if it is of any help:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("You should be pointing out the key (in string) as a command line\n");
return 1;
}
int ki = 0;
string k = argv[1];
int kl = strlen(k);
for (int ii = 0; ii < kl;ii++)
{
if (isalpha(k[ii]) == 0)
{
printf("Sorry, I can only work with alphabetic letters\n");
return 1;
}
}
string p = GetString();
for (int i = 0, n = strlen(p); i < n; i++)
{
ki = ki % kl;
if (isalpha(p[i]))
{
if (isalpha(k[ki]))
{
if (isupper(k[ki]))
{
k[ki] = k[ki] - 'A';
}
else
{
k[ki] = k[ki] - 'a';
}
}
//else
//{
//printf("\n%c\n",k[ki]);
//printf("Hey it'sa me, Buggio!\n");
//return 1;
//}
if (p[i] + k[ki] > 'z' || (p[i] + k[ki] > 'Z' && p[i] + k[ki] < 'a') || (isupper(p[i]) && p[i] + k[i] > 90))
{
p[i] = p[i] + k[ki] - 26;
printf("%c", p[i]);
}
else
{
printf("%c", p[i] + k[ki]);
}
ki++;
}
else
{
printf("%c", p[i]);
}
}
printf ("\n");
return 0;
}
Thanks in advance and have a good day =o