persiandrunkard
persiandrunkard

Reputation: 1

I was doing the problem set2 for cs50x course, and while solving the caesar problem, i came across some unsual ouputs

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

    int main(int argc,string argv[]){    

        if(argc==2)  //to make sure there's only one string after ./caesar
        {
        int digit=0,key=0;

            for(int i=0,n=strlen(argv[1]);i<n;i++)
            {
                if(argv[1][i]>='0' && argv[1][i]<='9')
                {
                digit= argv[1][i]-'0';
                key=key*10+digit;
                }

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

        }

        string text=get_string("plainttext:  ");
        printf("ciphertext: ");


        key=key%26;

        for(int i=0,n=strlen(text);i<n;i++){

            if(islower(text[i]))
            {

            text[i]=text[i]+key;

                if(text[i]>'Z')
                {
                    text[i]=text[i]-26;
                }


                printf("%c",(char)text[i]);




            }

        else if(isupper(text[i]))
            {
                text[i]=text[i]+key;

                if(text[i]>'Z')
                {
                    text[i]=text[i]-26;
                }


                printf("%c",(char)text[i]);
            }

            else
            printf("%c",(char)text[i]);


        }

        printf("\n %i",key);




        return 0;

        }







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

Design and implement a program, caesar, that encrypts messages using Caesar’s cipher.

https://cs50.harvard.edu/x/2024/psets/2/caesar/

this is the link to the problem

basically when I try "world, say hello" as my plaintext and 12 as key it doesn't act like it should, it gives out random output for w and y. like weird question marks in place of it`

Upvotes: 0

Views: 40

Answers (1)

NoDakker
NoDakker

Reputation: 4816

In testing out your code, a couple of things cropped up which probably were leading you to your issue. In both instances, the block of code in question is within your lowercase testing of the cipher text.

if(islower(text[i]))
{

text[i]=text[i]+key;

    if(text[i]>'Z')
    {
        text[i]=text[i]-26;
    }


    printf("%c",(char)text[i]);

First off, testing should be done against the lower case 'z' and not the uppercase 'Z'.

Second, ASCII characters are in the range of 0 to 127, and possibly adding a large cipher amount to the entered character can blow by the value of 127 giving you the unpredictable results you got.

With that, following is one path of refactoring for lower case encryption where the value of the derived character is checked and is adjusted down first before applying the cipher key.

if(islower(text[i]))
{
    if((text[i] + key) > 'z')   /* Since the value can go past the ASCII range, check for that first    */
    {
        text[i]=text[i] - 26;
    }
    text[i] += key;
    printf("%c",text[i]);
}

Testing this out in the terminal using a similar phrase appears to output the desired result with the proper wrap-around.

craig@Vera:~/C_Programs/Console/caes/bin/Release$ ./caes 12
Plain text: world,hello
ciphertext: iadxp,tqxxa
12

The takeaway from this is to probably research some more "C" tutorials especially in regard to using and manipulating ASCII characters.

Upvotes: 0

Related Questions