Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <string.h>
- #include <ctype.h> //alpha/digit functions
- #include <stdlib.h> //for atoi function
- 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
- {
- //Checks argc, the count of arguments. There should be 2, ./caesar and a key.
- if (argc != 2)
- {
- printf ("Usage: %s key\n", argv[0]);
- return 1;
- }
- int k = atoi (argv[1]);
- for (int i = 0; i < strlen(argv[1]); i++) //Runs through whole argv[1] string to check if it's acceptable
- {
- if (isdigit(argv[1][i]) == false || k < 0)
- {
- printf ("Usage: %s key\n", argv[0]);
- return 0;
- }
- else //the string consists solely of digits, convert to int
- {
- printf ("Success! Your key is %i\n", k);
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement