0

I am using character array and none of the test cases matches the result. Please help me find what is missing.

:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
    expected "ciphertext: b\...", not "ciphertext: b\..."
:( encrypts "barfoo" as "yxocll" using 23 as key
    expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
    expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
    expected "ciphertext: Fe...", not "ciphertext: Fe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
    output not valid ASCII text
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
    expected "ciphertext: ia...", not "ciphertext: ia..."
:) handles lack of key
:) handles non-numeric key
:) handles too many arguments

Here is my code.

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

int main(int argc, string argv[])
{
// check for only 2 arguments
if (argc == 2)
{
    int key = atoi(argv[1]);
    int notDig = 1;

    // check for digits in the argument
    for(int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if(!isdigit(argv[1][i]))
        {
            notDig = 0;
        }
    }
    if (notDig)
    {
    string text = get_string("plaintext: ");
    int ciLen = strlen(text);
    char ciText [ciLen];
        for (int i = 0; i < strlen(text); i++)
        {
            if (text[i] >= 'a' && text[i] <= 'z')
            {
                    //for lowercase
                    if (text[i] + key > 122)
                    {
                    int thisChar = (text[i] + key)% 122+96;
                    ciText [i] = thisChar;
                    }
                    else
                    {
                    int thisChar = (text[i] + key)% 122;
                     ciText [i] = thisChar;
                    }
            }
            else  if (text[i] >= 'A' && text[i] <= 'Z')
            {
                    //for uppercase
                    if (text[i] + key > 90)
                    {
                     int thisChar = (text[i] + key)% 90+64;
                      ciText [i] = thisChar;
                    }
                    else
                    {
                     int thisChar = (text[i] + key)% 90;
                     ciText [i] = thisChar;
                    }
            }
            else
            {
            // for other characters
            ciText [i] = text[i];
            }
        }

            printf("ciphertext: %s",ciText);
        }
        else
        {
            return 1;
        }
    }
        else
        {
            return 1;
        }
    return 0;
}
0

I've been looking at your calculations and none of the formulas make any sense to me.

The logic for encoding a letter should be this:

  1. Convert the ASCII code of the letter to a number from 0 to 25 inclusive.
  2. add the key. If the result is >25, then apply modulo to shift it down into the range of 0 to 25.
  3. Convert the result back to an ASCII code having the same case as the original letter.

That's all there is to it.

1
  • i have done the same thing. Instead of assigning the letter 0 to 25 i have directly made the calculations. In lowercase ASCII 122 is the final value ( ASCII of z) checking if the character+key > 122 (character+key) % 122 + 96 // 96 is first value (a) In uppercase ASCII 90 is the final value ( ASCII of Z) checking if the character+key > 90 (character+key) % 90 + 64 // 64 is first value (A) In normal convention 25 is the final value (value of z or Z) checking if the character+key > 25 (character+key) % 25 + 0 // 0 is the first value – user7968702 Sep 5 '20 at 15:18

You must log in to answer this question.

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