0

include

include

include

include

include

int main(int argc, string argv[]) { int condition = true;

    for(int i = 0, n =(strlen(argv[1])); i < n; i++)// going through string

    {

        if(argc !=2 && !isdigit(argv[1][i]))// set condition
            {

                condition = false;
            }

        if (condition == false)//check for wrong input

            {

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

        else if(condition == true)

            {

                string s = get_string("plaintext: ");// get text
                printf("ciphertext: ");

                for ( i = 0; i < strlen(s); i++)

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

                        if (islower(s[i]))
                            {

                                printf("%c", (((s[i] - 'a') + k) % 26) + 'a'); // rotating lower cases
                            }
                        else if (isupper(s[i]))
                            {

                                printf("%c", (((s[i] - 'A') + k) % 26) + 'A'); // rotating upper cases

                            }
                        else

                            {

                                printf("%c", s[i]); // printing other chars

                            }
                    }

            }

        printf("\n");

    }
        return 0;

}

0

If the input is 2x, how many times will this loop execute?

for(int i = 0, n =(strlen(argv[1])); i < n; i++)// going through string

It will encipher the text with 2 then give the usage message.

While it is the right approach to loop through each character of the key, that should be a standalone loop.

And beware: program access this strlen(argv[1]) before it knows if argv[1] exists (argc !=2)

1
  • Thank you very much for your help Apr 16 '20 at 21:51

You must log in to answer this question.

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