(I am very new to both coding and C.)
I want to check if the nr one string in "string argv" is one or more decimals and if it is convert it to an integer. I think I may need to iterate over the string but so far this code is not working. It says segmentation fault.
This is my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
int main (int argc, string argv [])
{
//check command line
if (argc == 2)
{
printf("success");
}
else
{
printf("Usage: ./caesar key.");
}
//validate number
if (isdigit (argv[1]))
{
int x = atoi(argv[1]);
printf("I is now %i\n", x);
return 0;
}
if (isalpha(argv[1]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
array is bigger than twois not standard C. Usestrlen()with the name of your array and compare the result with the number2by the mean of this binary operator:>isdigittests whether a single character is a digit. If you want to test whether all characters in a string are digits, you must loop over the string. The functionatoitries to convert the whole string to a number. (The loop is in the function itself.) There is another function,strtol, which converts a string to along. It has better possibilities for error checking.