title: showing Segmentation fault in hacker rank
body: <p>Below error is showing from compiler(Hackerrank c compiler).Here a is the pointer and assinged a starting address of memory block through malloc and given the input a string at runtime.Could any one help me?</p>
  
  <pre><code>GDB trace:
  Reading symbols from solution...done.
  [New LWP 4659]
  Core was generated by `solution'.
  Program terminated with signal SIGSEGV, Segmentation fault.
  #0  main () at solution.c:22
  22      if ((a[i] != a[j]) &amp;&amp; (i &gt;= 0 &amp;&amp; j &lt; n)) {
  #0  main () at solution.c:22
  </code></pre>
  
  <p>and my code is</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  int main() {
      int n, k, i, j, mid;
      char *a;
      int *l;
      a = (char *)malloc((n + 1) * sizeof(char));
      scanf("%d", &amp;n);
      scanf("%d", &amp;k);
      scanf("%s", a);
      mid = (0 + (n - 1)) / 2;
      if (n % 2 == 0) {
          i = mid;
          j = mid + 1;
          l = (int *)malloc((n / 2) * sizeof(int));
      } else {
          i = mid;
          j = mid;
          l = (int *)malloc((n / 2 + 1) * sizeof(int));
      }
  
      while (k != 0) {
          if ((a[i] != a[j]) &amp;&amp; (i &gt;= 0 &amp;&amp; j &lt; n)) {
              if (a[i] &gt; a[j]) {
                  a[j] = a[i];
                  k--;
                  i--;
                  j++;
              } else {
                  a[i] = a[j];
                  k--;
                  i--;
                  j++;
              }
          }
      }
      printf("%s", a);
  }
  </code></pre>