title: CS50 - Problem Set 2 - encountering "segmentation fault (core dumped)"
body: <p>My program seems to be working fine and it does as intended, it takes command line arguments and &quot;rotates&quot; the inputted string from the prompt depending on the inputted command line argument. However, if I run my code without any arguments like: <code>./caesar</code> it doesn't work, it says &quot;segmentation fault (core dumped)&quot; but if i run it like this <code>./caesar 1</code> or any other number, it works as intended.</p>
  <pre><code>#include &lt;cs50.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  
  int only_digits(string n);
  char rotate(char i, int n);
  
  int main(int argc, string argv[])
  {
  
  
      if(only_digits(argv[1]) &amp;&amp; argc == 2) {
          string plaintext = get_string(&quot;plaintext:  &quot;);
          printf(&quot;ciphertext: &quot;);
          int k = atoi(argv[1]);  // converts string n to k
  
          for (int i = 0, l = strlen(plaintext); i &lt; l; i++)
          {
              printf(&quot;%c&quot;, rotate(plaintext[i], k));
  
          }
  
          printf(&quot;\n&quot;);
      }
  
      else
      {
          printf(&quot;Usage: ./caesar key\n&quot;);
          return 1;
      }
  }
  
  
  
  int only_digits(string n) // function that returns 1 or 0 depending if given string is only digits
  {
      int state;
      for(int i = 0, l = strlen(n); i &lt; l; i++)
      {
          if (isdigit(n[i]))  // checks if characters in string is a digit from 1 to 9
          {
              state = 1;
          }
          else
          {
              state = 0;
              break;
          }
      }
      return state;
  }
  
  char rotate(char c, int n)
  {
      char rotated_char = c;
      if (isupper(c))
      {
          int a_index = c - 'A';
          int c_cipher = (a_index + n) % 26;
          rotated_char = c_cipher + 'A';
      }
      else if (islower(c))
      {
          int a_index = c - 'a';
          int c_cipher = (a_index + n) % 26;
          rotated_char = c_cipher + 'a';
      }
      return rotated_char;
  }```
  </code></pre>