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

int cypher(string k, char p);

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    int l = 0;
    l = strlen(argv[1]);
    for (int i = 0; i < l; i++)
    {
        if (isalpha(argv[1][i]) != 0)
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
        else if (isdigit(argv[1][i]) == 0)
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }

    string a = get_string("plaintext:  ");
    int x = strlen(a);
    char b[x];
    char c[x];
    for (int j = 0; j < x; j++)
    {
        b[j] = a[j];
        c[j] = cypher(argv[1], b[j]);
    }
    printf("ciphertext: %s\n", c);
} // Main ends

int cypher(string kk, char pp)
{
    int aa = pp - 0;
    int ff;
    if (isalpha(pp) == 0)
    {
        return aa;
    }
    else if (pp >= 'A' && pp <= 'Z')
    {
        int bb = aa - 65;
        int cc = atoi(kk);
        int dd = bb + cc;
        int ee = dd % 26; // Remainder added to 26 alphabets.
        ff = ee + 65;
        return ff;
    }
    else
    {
        int bb = aa - 97;
        int cc = atoi(kk);
        int dd = bb + cc;
        int ee = dd % 26;
        ff = ee + 97;
        return ff;
    }
}
OUTPUT

:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:) encrypts "barfoo" as "yxocll" using 23 as key
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:) encrypts "barfoo" as "onesbb" using 65 as key
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
    output not valid ASCII text
:) handles lack of key
:) handles non-numeric key
:) handles too many arguments
1
  • I have fixed it by printing individual characters instead of whole string. – Vsjain May 11 '20 at 12:19
-1

So, what I'm seeing is that the code passed all the tests except the ones with non-alpha characters like numbers, punctuation and spaces?

Hmmm.... I wonder what the problem could be. ;-)

Also, since caesar is only supposed to take a number as a key, why does it check for alphas? It only needs to check that the key is composed of digits.

On a side note, the code sure seems to have a lot of variables. I don't understand why it creates so many variables that are just copies of other variables when you could just use the originals. Having all the duplicate vars is inefficient and makes for very difficult code to read.

There's a lot of room to make this code a lot simpler.

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

2
  • How should I fix the problem of getting a non valid ASCII text? – Vsjain May 9 '20 at 6:33
  • It's in the instructions. Caesar should encode alphas and just copy the nonalphas. – Cliff B May 9 '20 at 17:35
-1

Might be a problem with the special characters: a ciphertext character should be equal to plaintext character if the character is not alphabetic. if special characters are treated as alpha the ASCII of the characters might be out of range of the ASCII characters so maybe you can add an 'else if' for special characters that return the same plain text character if "isalpha" is false

You must log in to answer this question.

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