2

Details about my goal

In C, my program is supposed to take an input from command line, read it, make sure that it is a digit and then print out "Success" if true, "Usage: ./caesar key" if false.

Expected results

./caesar 33
Success

or

./caesar zzz
Usage: ./caesar key

Actual results

./caesar 33
Segmentation error

What I've tried

 int main (int argc, string argv[])
 {
   if (argc == 2)
   {
      if (isdigit(argv[1]))
      {
        printf("Success\n");
        return 0;
      }

or

if (argc == 2)
{
  if (isdigit(argv[0][1]))
  {
    printf("Success\n");
    return 0;
 }

or

int main (int argc, string argv[])
{
   if (argc == 2 && isdigit(argv[1]))
   {
     printf("Success\n");
     return 0;
   }
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Nermin
  • 47
  • 1
  • 8

3 Answers3

5

What you are doing wrong here is that you want to check if the entire string consists of digits, but you are passing the string itself to isdigit, which expects a char. What you can do is manually iterate through the char array and check if each char is a digit, or you can use the strspn function as demonstrated below.

strspn(argv[1], "0123456789") == strlen(argv[1])

This will compare the span of digits in the argument and return true if it is the same as the length of the string itself. If you want to allow + and - signs in the argument, use "-+0123456789" inside strspn, though that would result in a false positive for cases like +++++ (as mentioned by @dreamcrash below), which is why defining a custom function is always a better option. Both the strspn and strlen functions are available in the string.h header.

Param Siddharth
  • 858
  • 2
  • 7
  • 20
2

If you do not need to use the isdigit function, then you can use the strspn function as suggested by @Param Siddharth. However, they are several corner-cases with using

if (strspn(string, "-+0123456789") == strlen(string))

for instance that the values like "-----" would be considered as a valid number.

If you want to use the isdigit function than you need to go through all the chars in the String (instead of passing the entire String in one go) and check if each one of them is indeed a digit:

    ...
    int i, is_number = 1;
    for (i = 0; is_number && argv[1][i] != '\0'; i++){
        if(i == 0 && (argv[1][i] == '-' || argv[1][i] == '+')){
           continue;
        }   
        is_number = isdigit(string[i]);
     }
     // It should fail for Strings that are : empty, only "-" or "+"
    if(i == 0 || (i == 1 && !isdigit(argv[1][0])) || !is_number){
        printf("The String is not a Number \n");
        return 1;
     }
     printf("Success\n");
     return 0;
}

I tested with the inputs:

  • "-", not a number
  • "", not a number
  • "-1", number
  • "+1", number
  • "+1+1" not a number
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
1
int main (int argc, string argv[])

string data type is not defined in C. Second argument should be char* argv[].

Please see the answer :https://stackoverflow.com/questions/14709323/does-c-have-a-string-type#:~:text=There%20is%20no%20string%20type,one%20additional%20zero%20terminating%20character.

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
  • 1
    `string` is a `typedef` for `char *` in Harvard's CS50 course. See the [tag info for CS50](https://stackoverflow.com/tags/cs50/info). – user3386109 Nov 17 '20 at 09:11
  • 1
    I still don't understand why they don't just use char * but, what do I know, I'm a beginner. – Nermin Nov 17 '20 at 09:46