CS50 Caesar how do I atoi?

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");
  }

Example at onlinegdb here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top