0

Can anyone help me out why my code won't return any cipher text, think it has something to do with the formula ci = (pi + k) % 26 I have done. Any help much appreciated.

// Libraries
#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, string argv[])

{

    // If Argc count is not 2 then print error
    if (argc != 2)
        {

            printf("Usage: ./caesar key\n");
            return 1;
        }
    // Iterate over each character in Argv1 and if it's not a digit then print error
    for(int i = 0, len = strlen(argv[1]); i < len; i++)
        {
            char n = argv[1][i];
            int digit = isdigit(n);

            if (!digit)
                {
                    printf("Usage: ./caesar key\n");
                    return 1;
                }
        }

    // Get input from user and iterate over each character, then change it by a certain digit
    string text = get_string("plaintext: ");
    for(int j = 0, length = strlen(text); j < length; j++)
        {
           char c = text[j];

           // Formula to keep cipher inside the Alphabet
           char ci = (text[j] + atoi(argv[1])) % 26;
           // Preserving each of the alpha characters upper or lower
           if (isalpha(ci))
           {
               printf("%c", ci);
           }

           else if (isupper(ci))

           {
               printf("%c", isupper(ci));
           }

           else if (islower(ci))
           {
               printf("%c", islower(ci));
           }

        }
}
1
  • I think your equation is wrong, it's true that your formula is correct but remember we are using ASCII here so if the character 'A' is 65 in ASCII table so if you do your equation it uses 65... the equation here treats 'a' here is supposed to be represented as 1 and b as 2, but it's not like that in the ASCII right..
    – Ojou Nii
    Commented Apr 7, 2020 at 15:29

1 Answer 1

1

In your code where the plaintext gets converted to ciphertext, each of the characters in the plaintext string is trying to add an integer value to a character type and then divides the sum by 26 to get a remainder that is a decimal point. When you try to map that result with the ASCII characters, you get nothing returned as there is nothing that maps to a decimal number in ASCII. Therefore, you need to subtract the ASCII value of character 'a' or 'A' from each character in the plaintext, add the key to it, divide the sum by 26 and add the ASCII value of 'a' or 'A' into that remainder. For example: say, key=1; plaintext="a"; plaintext[i] = 'a'; ASCII value of 'a' = 97; putting that into the formula above: (((97 - 97) + 1) % 26) + 97 ~ 98; Hence, converting 97 back to a char type gives 'b'. This works for all and any of the alphabets with any positive key value provided! Let me know if further help is needed!

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

int main(int argc, string argv[])

{

    // If Argc count is not 2 then print error
    if (argc != 2)
        {

            printf("Usage: ./caesar key\n");
            return 1;
        }
    // Iterate over each character in Argv1 and if it's not a digit then print error
    for(int i = 0, len = strlen(argv[1]); i < len; i++)
        {
            char n = argv[1][i];
            int digit = isdigit(n);

            if (!digit)
                {
                    printf("Usage: ./caesar key\n");
                    return 1;
                }
        }

    // Get input from user and iterate over each character, then change it by a certain digit
    string text = get_string("plaintext: ");
    printf("cipertext: ");
    for (int i = 0; i < strlen(text); i++)
        {
            // Converts all the lower letter alphabets
            if (text[i] >= 'a' && text[i] <= 'z')
            {
                printf("%c", ((((text[i] - 'a') + atoi(argv[1])) % 26) + 'a'));
            }
            // Converts all the upper letter alphabets
            else if (text[i] >= 'A' && text[i] <= 'Z')
            {
                printf("%c", ((((text[i] - 'A') + atoi(argv[1])) % 26) + 'A'));
            }
            // Returns any character other thatn alphabets as such
            else
            {
                printf("%c", text[i]);
            }
        }
}

You must log in to answer this question.

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