0

:( handles non-numeric key timed out while waiting for program to exit the program:

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

int main(int argc, char** argv)
{
   if ( argc != 2 )
   {
      printf ("./caesar key\n");
      return 1;
   }

   int key = atoi(argv[1]);

   if (key < 0)
   {
       printf ("the key must be a positive number\n");
       return 1;
   }

   if (isalpha(key))
    {
        printf ("./caesar key\n");
      return 1;
    }

   string a = get_string("plain text : ");

       printf ("ciphertext: "); 

   for(int i = 0, len = strlen(a); i < len; i++)

       if (islower(a[i]))

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

       else if (isupper(a[i]))

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

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

printf("\n");

}
  • Try the program with 2b as the key to see what happens. Does it "reject non-numeric key"? Read through man atoi again to see why that might be the case. – DinoCoderSaurus May 7 '20 at 11:57
0

Why are you using so many loops and conditional statements in your code? Well, here's an example of my solution. Focus on the part that validates the 'key'. Let me know if there's any part of it confusing you!

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

int main(int argc, string argv[])
{
    // Confirms there are two arguments included to run the program
    if (argc == 2)
    {
        // Iterates on the key in command line and ensures it is a positive integer
        for (int i = 0; i < strlen(argv[1]); i++)
        {
            // Returns error message if key includes anything other than digits
            if (isdigit(argv[1][i]) == false)
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }
        int k = atoi(argv[1]);

        string plaintext = get_string("plaintext: ");
        printf("ciphertext: ");

        // Reads the plaintext and converts it it ciphertext
        for (int i = 0; i < strlen(plaintext); i++)
        {
            // Converts all the lower letter alphabets
            if (plaintext[i] >= 'a' && plaintext[i] <= 'z')
            {
                printf("%c", ((((plaintext[i] - 'a') + k) % 26) + 'a'));
            }
            // Converts all the upper letter alphabets
            else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')
            {
                printf("%c", ((((plaintext[i] - 'A') + k) % 26) + 'A'));
            }
            // Returns any character other thatn alphabets as such
            else
            {
                printf("%c", plaintext[i]);
            }
        }
        printf("\n");
        return 0;
    }
    // Returns an error message if the code isn't executed in proper format, i.e, "./caesar key"
    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 .