I suggest using a loop to check each digit of the string, then use a single call to atoi
to convert the string to a number:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
//if not exactly 1 argument, print error message a value of 1
if (argc == 2)
{
for (int i = 0; i < strlen(argv[1]); i++)
{
if (!isdigit(argv[1][i]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
int a = atoi(argv[1]);
printf("%i\n", a);
}
else
printf("Usage: ./caesar key\n");
}
CLICK HERE to find out more related problems solutions.