Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <ctype.h> //alpha/digit functions
  5. #include <stdlib.h> //for atoi function
  6.  
  7. int main(int argc, string argv[]) //where argc is count of arguments e.g. ./caesar 3 --> argc = 2, and string argv lists the args, e.g. argv[0] = ./caesar and argv[1] = 3
  8. {
  9.         //Checks argc, the count of arguments. There should be 2, ./caesar and a key.
  10.         if (argc != 2)
  11.         {
  12.             printf ("Usage: %s key\n", argv[0]);
  13.             return 1;
  14.         }
  15.    
  16.     int k = atoi (argv[1]);
  17.    
  18.     for (int i = 0; i < strlen(argv[1]); i++) //Runs through whole argv[1] string to check if it's acceptable
  19.     {
  20.         if (isdigit(argv[1][i]) == false || k < 0)
  21.         {
  22.             printf ("Usage: %s key\n", argv[0]);
  23.             return 0;
  24.         }
  25.        
  26.         else //the string consists solely of digits, convert to int
  27.         {
  28.            
  29.             printf ("Success! Your key is %i\n", k);
  30.             return 0;
  31.         }
  32.        
  33.     }
  34.    
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement