0
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)

3 Answers 3

1

There is a nice function toupper, look what it does. Alternatively, you can check if a char in key is less than 91 and if not, subtract 32,

1
  • Not sure if this is what the walkthrough was on about, but thanks very much! Works and I'll keep this neat trick in mind in the future!
    – Knovolt
    Commented Jun 13, 2017 at 0:41
0

A good idea would be to create an array of ints, with the same length as the key, and populate it with the numeric representation of the key. You already have solved the part where you discriminate between uppercase and lowercase letters in order to find if you have to subtract 65 or 97. Just do the same thing to the key first.

For example if the key is bAcOn, you can check for each letter if it's lowercase or uppercase, subtract the appropriate value, and create the numeric key [1, 0, 2, 14, 13], and then you don't have to worry about converting to ints again or if it was uppercase or lowercase.

Happy coding!

1
  • Is the function in the update along the lines of what you're saying?
    – Knovolt
    Commented Jun 13, 2017 at 0:19
0

There are going to be four cases for this problem as : input string ith charachter upper case with key jth charachter upper or lower case or input string ith charachter lower case with key jth charachter upper or lower case. Now , you should think of a way(conditional) whereby you can consider each of the case and accordingly calculate the integer value of each output charachter in the ciphertext. Hope that helps.Happy coding!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .