for (int i = 0, j = strlen(s), k = 0; i < j; i++, k++)
{
if (isalpha(s[i]))
{
if (isupper(s[i]))
{
if (argv[1][k] != '\0')
{
printf("%c", (((s[i] - 65) + (argv[1][k] - 65) % 26) + 65));
}
else
{
k = 0;
printf("%c", (((s[i] - 65) + (argv[1][k] - 65) % 26) + 65));
}
}
else if (islower(s[i]))
{
if (argv[1][k] != '\0')
{
printf("%c", (((s[i] - 97) + (argv[1][k] - 65) % 26) + 97));
}
else
{
k = 0;
printf("%c", (((s[i] - 97) + (argv[1][k] - 65) % 26) + 97));
}
}
}
else
{
printf("%c", s[i]);
}
}
This is one thing I'm stuck on. How do I shift the characters by the same integer ignoring whether the key is in upper or lower case? For example, how do I make it so that "B" and "b" both have a key shift of 1?
In the code above, I can't think of a way to confirm that if argv[1][k]
is an upper or lower case character. As far as I know, the code works great if the key that is inputted is all in capitals - say "B". But, it'll all go wrong as soon as I put a lower case key(s).
I think I need to have argv[1][k] - 97
instead when argv[1][k]
is lower case, but I'm not sure how to detect if it is.
Update:
int check_key(string v)
{
int key[strlen(v)];
for (int i = 0, j = strlen(v); i < j; i++)
{
if (isalpha(v[i]))
{
if (isupper(v[i]))
{
key[i] -= 65;
return key;
}
else
{
key[i] -= 97;
return key;
}
}
}
}
and within the for loop, replacing (argv[1][k] - 65) % 26)
is:
(check_string(argv[1])[i] % 26)