0

I am not getting the desired output when I want cipher the plaintext with key 13 with long sentence with commas and space. Please help, thank you.

    //Program to encrypt the message by using Caesar's cipher
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main(int argc, string argv[])
{
    //make sure the program was run with command-line argument
    if (argc == 2)
    {
       //validate the key
       int key = atoi(argv[1]);
       int len = strlen(argv[1]);
        // iterate over the key 
        for (int i = 0; i < len; i++)
        {
            if (!isdigit(argv[1][i]))
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
            else if (isalpha(argv[1][i]))
            {
                printf("Usage: ./caesar key");
                return 1;
            }
        }

        // prompt user for plaintext
        string plaintext = get_string("plaintext: ");
        printf("ciphertext: ");

        // iterate over the plaintext
        int length = strlen(plaintext);
        for (int i = 0; i < length; i++)
        {
            if (islower(plaintext[i]))
            {
                printf("%c",plaintext[i] + 'a' + key % 26 - 'a');
            }
            else if (isupper(plaintext[i]))
            {
                printf("%c", plaintext[i] +'A' + key  % 26  - 'A');
            }
            else
            {
                printf("%c", plaintext[i]);
            }
        }
        printf("\n");
    }
    else
    {
        printf("Usage: ./caeasr key\n");
        return 1;
    }
    return 0;
} 

1 Answer 1

0

I am a bit confused by the for loop.

for (int i = 0; i < length; i++)
    {
        if (islower(plaintext[i]))
        {
            printf("%c",plaintext[i] + 'a' + key % 26 - 'a');
        }
        else if (isupper(plaintext[i]))
        {
            printf("%c", plaintext[i] +'A' + key  % 26  - 'A');
        }
        else
        {
            printf("%c", plaintext[i]);
        }

What happens when plaintext[i] + key is larger than 90 or 122? You need to implement a code that makes sure the ciphertext stays with in that range. Im sure there are more then one way to do this be you can implement a loop that subtracts 26 if the value of plaintext[i] + key equal more then 90 or 122.

So apart from that, I can see there are a couple of odd things in your code.

for example

else if (isalpha(argv[1][i]))
        {
            printf("Usage: ./caesar key");
            return 1;
        }

What if the key is neither alpha or digit ? like "<" or "?"

Also you are reducing the efficiency of the program by making it more complicated than it needs to be.

If you turn the else if to an else you will fix that issue.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • Thank you very much. But I remove the else if statement but the bug still exit.
    – BMavhungu
    Commented Nov 4, 2019 at 9:22
  • Sorry for the mis understanding. The bug is due to the fact that your code allows c + key to excede 90 and 122. You can implement a loop that checks if it’s higher and then subtracts 26 if it is.
    – Chris
    Commented Nov 5, 2019 at 10:26
  • Thank you very much. Now it is after I followed every instruction you instructed me yesterday. And I found out the bug was on my formula. I am already submitted the pset and I got 99%. Again thank you very much. The previous one was 49%. Thank you.
    – BMavhungu
    Commented Nov 5, 2019 at 15:11

You must log in to answer this question.

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