# Vigenere - is there a simpler logic behind it?

My code for vigenere.c works for check50, but ends up being much longer than caesar.c in the encipher part.I was wondering if I was missing a simple point and have meandered through the logic behind it? I couldn't find another better way to avoid jumping to the next element of key shift if the character is not alphabetic!

I'm sure there is a less messy solution to vigenere?

Here is the encipher part of my code for vigenere.c

``````int i = 0;
int l = -1;

do
{
if (isalpha (p[i]))
{
do{
l++;
if (isupper (p[i]))
{
printf ("%c", (p[i] - 65 + k[l%m])% 26 + 65);
}
else
{
printf ("%c", (p[i] - 97 + k[l%m])% 26 + 97);
}
i++;
}while (isalpha(p[i]));
}

else
{
printf ("%c", p[i]);
i++;
}
} while ( i < n);
``````
• Ok I managed to shorten it up a bit this morning after some hours of sleep.....obvsiously the do {.......; i++;} while (i < n); can be substituted by a for loop statement. Other than that, is there a way to avoid introducing another counter variable (l in my case)? Jun 13, 2017 at 11:22
• I did mine using 2 counters as well, I think that's how most people did it. I don't even think there's another way. also I suggest you use: 'a' instead of 97 and 'A' instead of 65 to minimise "magic numbers" Jun 15, 2017 at 14:54