0

My code compiles and it seems to handle the encryption of the given text correctly...but not according to the cs50 check. Here's the result of the check:

:) 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
\ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 
31: 196..."

:) handles lack of argv[1]

So my question is...

My code can't handle the long text of "world, say hello!" It seems it has a segmentation fault when n > 16 (n being the length of a string). Is there a way I can set my memory so that the program can handle a string with more than 16 characters?

Here's my code if it helps:

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Please enter 2 arguments only\n");
        return 1;
    }

else
    {
        string key = argv[1];
        int k = atoi(key);

    if (k < 0)
        {
        printf("Please provide a non-negative integer\n");
        return 1;
        }

    else
        {
            string message = GetString(); 
            int n = strlen(message);
            int i = 0;
            int m[i], mm[i], crypt[i], c[i], cc[i], cryptic[i];

            for (i = 0; i < n; i++)
                {
                    if (isalpha(message[i]))
                        {
                            if (isupper(message[i]))
                                {
                                    m[i] = message[i] - 65;
                                    crypt[i] = ((m[i] + k) % 26);
                                    c[i] = crypt[i] + 65;
                                    printf("%c", c[i]);
                                }
                            if (islower(message[i]))
                                {
                                    mm[i] = message[i] - 97;
                                    cryptic[i] = ((mm[i] + k) % 26);
                                    cc[i] = cryptic[i] + 97;
                                    printf("%c", cc[i]);            
                                }
                        }

                    else
                        {
                            printf("%c", message[i]);
                        }
                }   
        printf("\n");
        }
    }

}

1
  • Well, color me embarrassed. I disapproved an edit and then figured out that it's an edit to a nearly identical looking question. I should have approved the edit. oops! ;-)
    – Cliff B
    Sep 9 '18 at 1:02
1
            int i = 0;
            int m[i], mm[i], crypt[i], c[i], cc[i], cryptic[i];

You're creating arrays of length 0. You could fix that by using n instead of i for array dimensions, but better not have arrays there (you throw away the result after the current iteration, so why keep those numbers?)

That's also an impressive number of variables for what's essentially

printf("%c", (message[i] - 'A' + k) % 26 + 'A');

and

printf("%c", (message[i] - 'a' + k) % 26 + 'a');

('A' and 65 are the same here)

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

int main(int argc, string argv[])
{
    int key, c = 0;
    bool isnum;
    char a;
    char sum[100];



    if (argc == 2)
    {
        for (int j = 0 ; j < strlen(argv[1]); j++)
            // this for loop is to look after the command line arguments which are of the form 20x or 4g etc.
        { 
            if (isdigit(argv[1][j]))
            {
                isnum = true;
            }
            else
            {
                isnum = false;
            }
        }
        
        if (isnum == true && atoi(argv[1]))
        {
            key = atoi(argv[1]);
            string plain;
            plain = get_string("plaintext: ");
            for (int i = 0; plain[i] != 0 ; i++)
            {
                if (isalpha(plain[i]))
                {
                    if (isupper(plain[i]))
                    {
                        plain[i] = (int)plain[i] - 65;
                        
                        c = ((plain[i] + key)) % 26;
                        c = c + 65;
                        a = (char)c ;
                        strncat(sum, &a, 1);
                    }
                    else if (islower(plain[i]))
                    {
                        plain[i] = (int)plain[i] - 97;
                        c = (plain[i] + (key)) % 26;
                        c = c + 97;
                        a = (char)c;
                        strncat(sum, &a, 1);
                    }
                    
                }
                else
                {
                    strncat(sum, &plain[i], 1);
                }
            }

            printf("ciphertext: %s\n", sum);
       
            return 0;
        }
        else
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }
    else
    {
        printf("Usage: ./caesar key\n");
        return 1;

    }


}

You must log in to answer this question.

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