0

am having trouble printing the correct ciphertext, code is rather lengthy for sure but will appreciate it a lot if anyone can tell me what's the problem here!

For example with a key of 1 and a plaintext of 'hello', I'll end up getting a ciphertext of 'aaaaa' instead!! (No idea why)

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(int argc, string argv[])
{   
    if (argc != 2)
    {
        return 1;
    }
    else
    {
        for (int i = 0; i < strlen(argv[1]); i++)
        {
            if (isdigit(argv[1][i]) == 0)
            {
                return 1;
            }
        }
        char alphabets_upper[26];
        char alphabets_lower[26];
        for (int i = 0; i < 26; i++)
        {
            alphabets_upper[i] = 'A' + i;
            alphabets_lower[i] = 'a' + i;
        }
        int key = atoi(argv[1]);
        string s = get_string("plaintext: ");
        for (int i = 0; i < strlen(s); i++)
        {
            if (isalpha(s[i]) != 0)
            {
                // check upper/lower and preserve
                for (int index = 0; index < 26; index++)
                {
                    if (alphabets_upper[index] == s[i])
                    {
                        s[i] = alphabets_upper[(index + key) % 26];
                    }

                    if (alphabets_lower[index] == s[i])
                    {
                        s[i] = alphabets_lower[(index + key) % 26];
                    }
                }
            }
        }
        printf("ciphertext: %s\n", s);
    }
}

1 Answer 1

1

Curious way to solve Caesar, congratulations, at least it is original. Some things about your program. When using s [i], the same array that contains the plain text, you are making a mistake, the index "i" also affects s [i] in the for loop, not just the "index" index. So you're constantly overwriting the "s" array, so you're never going to get the correct encryption. As an easy solution we can use a temporary variable to store each encrypted character, the following works:

            if (alphabets_upper[index] == s[i])
                {
                    int cipher = alphabets_upper[(index + key) % 26];
                    printf("%c", cipher);
                }

                if (alphabets_lower[index] == s[i])
                {
                    int cipher = alphabets_lower[(index + key) % 26];
                    printf("%c", cipher);

                }

You have another problem, in your code, and that is that you must print non-alphabetic characters, such as a blank space for example. This sounds like an else, I do not want to spoil the fun, so think where you should put this else.

3
  • Haha, is that a good thing! Ah I see. I thought it would still work though even if I did not use a temporary variable, as I would be swapping out the character on that particular index 'i' in the plain text for a new character and that would not affect the positions of the subsequent characters.
    – Jereld Lim
    Commented Apr 19, 2019 at 11:48
  • Oh right I think I got it now!! Seems like the s[i] will get stuck in a loop where it keeps changing
    – Jereld Lim
    Commented Apr 19, 2019 at 12:08
  • 1
    When I'm not sure what a nested loop does, I usually pick up pencil and paper and write a few terms. Remember that a program does what we write, not what we think.
    – MARS
    Commented Apr 19, 2019 at 13:07

You must log in to answer this question.

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