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

    int main(int argc, string argv[])
    {
        string inpt = argv[1];
        int value = atoi(inpt);
        printf("%c\n", argc);
        if (argc != 2 || isalpha(inpt[0]))
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
        if (isalpha(inpt[1]))
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
        //printf("%s\n", inpt);
        string plaintext = get_string("plaintext: ");
        //printf("%c\n%i\n", inpt[1], value + 3);
        printf("ciphertext: ");
        for (int i = 0, n = strlen(plaintext); i < n; i++)
        {

            char x = plaintext[i];
            if (isalpha(x))
            {
                if (isupper(x))
               {
                    if ((int)x + value > 90)
                    {
                        int x1 = (int)x - 65;
                        int c = (x1 + value) % 26;
                        printf("%c", c + 65);
                    }
                    else
                    {
                        printf("%c", x + value);
                    }
                }
                 else if (islower(x))
                {
                    if ((int)x + value > 122)
                    {
                        int x2 = (int)x - 97;
                        int f = (x2 + value) % 26;
                        printf("%c", f + 97);
                    }
                    else
                    {
                        printf("%c", x + value);
                    }
                }
            }
            else
            {
                printf("%c", x);
            }
   
    }
        printf("\n");
    }
1

Does the code check to see that there is exactly one input parameter string BEFORE trying to do anything with argv[1]? Doesn't look like it.

Also, you might benefit from reading this: Do YOU know how to find a seg fault?? Advice to new programmers

3
  • If i try to add code before it, it just gives me a lot more errors, can u elaborate on what u are trying to say, maybe an example? – Avaneesh Tisgaonkar Aug 3 '20 at 23:50
  • The example is already in your code, just in the wrong order. The very first thing you should do is check the value of argc. If argc is NOT 2, then the program should terminate. Note: If argc = 1, then there is no argv[1] so anything that uses it will generate a seg fault! – Cliff B Aug 4 '20 at 1:07
  • Oh ok, that makes sense now. Thank you very much – Avaneesh Tisgaonkar Aug 4 '20 at 4:52

You must log in to answer this question.

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