0
// ciphering 
    for (int i = 0, n = strlen(plaintext); i < n; i++)
    {
...
        // lowercase
        if (plaintext[i] > '`' && plaintext[i] < '{')
        {
            if (plaintext [i] + key < '{')
            {
                ciphertext[i] = (plaintext[i] + key);
            }
            // overshift
            else if (ciphertext[i] + key > 122)
            {
                ciphertext[i] = (plaintext[i] + key);
                do
                {
                    ciphertext[i] -= 26;
                }
                while (ciphertext[i] > 122);
            }
        }

I have the same code directly above for uppercase, with '[' and 'Z' instead of '{' and 122 as appropriate. Directly below it is code that works for non-letters. When the code is applied for lowercase letters with a key of less than or equal to 52, it works. However, when applied to specifically lowercase letters with a key of more than 52, there is no return (i.e it returns like /200 which doesn't appear, per debug50 and using printf on a specific char.

0

else if (ciphertext[i] + key > 122) in this line (13th of the snippet you posted, including the "..." as a line), should that be plaintext[i]? It doesn't look like you've instantiated ciphertext[i] yet.

Also, what data type is ciphertext? If it's something with chars, remember that the int range of char is [-128, 127], so you might be seeing some overflow into negative numbers, which would definitely be < 122.

If neither of those help, can you post the working uppercase code, where you've declared the variables, and/or some sample working and not working inputs?

Also, to improve readability, consider rewriting

if (plaintext[i] > '`' && plaintext[i] < '{')

as

if (plaintext[i] >= 'a' && plaintext[i] <= 'z')

since it makes it much more clear what range of characters you're looking at. It would be less prone to bugs caused by using incorrect characters, since they would be obvious without looking up an ASCII table.

5
  • is there a way to double the range of an int char (while taking away negative range)? Like so that it could go up to like 200?
    – Matthew
    Jun 23 '20 at 1:02
  • When I try to make ciphertext into an unsigned char, it doesn't allow me to use ciphertext[i] bc it says that its no longer a vector or int
    – Matthew
    Jun 23 '20 at 1:15
  • let me add a few lines of code ``` string plaintext = get_string("plaintext: "); string ciphertext = strdup(plaintext); ```
    – Matthew
    Jun 23 '20 at 2:01
  • @Matthew I don't know if there's a way to double the positive range of a char, but since the problem says the key could be much larger than that, you should probably be working with a different data type that isn't as restricted in range
    – Llenk
    Jun 23 '20 at 14:18
  • I just figured it out. if I run the do/while loop before the line ciphertext[i] = plaintext[i] + key and instead put a line that sets ciphertext[i] = plaintext[i] before the loop, then make the while condition ciphertext[i] + key <= 'z', and after the condition have a line ciphertext += key, it works. basically I have to subtract before I add because the adding gets too high. I don't know if you are following, but thanks for the help because I completely forgot that int has a range only to 157
    – Matthew
    Jun 23 '20 at 15:25
0

Problem is in your else if statement you are using ciphertext[i] + key as a condition. Replacing ciphertext[i] + key with plaintext[i] + key should solve the problem.

You must log in to answer this question.

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