I've been trying to get my vigenere straight for some time now, managed to get rid of most of the bugs, but all I can see is that it still rotates the code for me even when I have a non-alpha characters. Could you please help me? Probably I declared j inside or outside of some loop i shouldn't have, but honestly, I have no idea. Any hint, help is highly appreciated. So this is what I managed to work out so far:
string text = GetString();
string keyword = argv[1];
for (int j = 0; j < strlen(keyword); j++)
{
keyword[j] = toupper(keyword[j]);
}
// i want the key to code with lowercase only. does that work?
for (int i = 0; i < strlen(text); i++)
{
if (isalpha (text[i]))
// for letters
{
int j = i % strlen(keyword);
if (isupper(text[i]))
{
int result = ((text[i] + keyword[j] - 2*'A') % 26) + 'A';
printf("%c", result);
}
else
// for lower
{
int result = (text[i] + keyword[j] -'A' - 'a') % 26 + 'a';
printf("%c", result);
}
}
else
{
printf ("%c", text[i]);
// should not proceed with the code letters but it still does
}
}
printf("\n");
// probably it shouldnt but prettier
return 0;