# Vigenere program is not working correctly!

I think the keyword is not repeating itself. Kindly help!

``````string p = GetString();
int a;
for (int i=0, n=strlen(p); i<n; i++)
{
if (isupper(p[i]))
{
if (isupper(k[i]))
a = k[i]-65;
else
a = k[i]-97;
int z = a%n;
int b = p[i]-65;
int c = (z+b)%26;
int d = c+65;
printf ("%c", d);
}
else if (islower(p[i]))
{
if (isupper(k[i]))
a = k[i]-65;
else
a = k[i]-97;
int z = a%n;
int b = p[i]-97;
int c = (z+b)%26;
int d = c+97;
printf ("%c", d);
}
else
{
printf ("%c", p[i]);
}
}
printf ("\n");
return 0;
``````
• First of all, I highly suggest you come up with descriptive names for your variables since you are using a lot of them and use at least a few comments particularly if you're going to ask people for help as it makes it a lot easier to follow what it is you're doing. looking over your code this line jumps out at me: ' int z = a%n;' Is this your attempt to rollover the key? if you gave descriptive names for your variables would probably noticed that one of them is based on the plain text while the other is based on the key. Is that what you intended? What is this line supposed to achieve?
– Evan
Jul 18 '16 at 21:54
• Thanks for the comment. I will try to use descriptive names in future. Yes, you are right, I attempted to rollover the key. Can you point out the problem? Jul 19 '16 at 10:58
• The problem is you are using the n variable which is the string length of the message rather than the key.
– Evan
Jul 19 '16 at 14:40