title: Counting characters in a string by dynamic memory allocation
body: <p>I am writing a C program where I write a function to count the number of character in a function. I have to use a dynamic memory allocation to allocate memory for an array take the array and copy it to another with fixed size. I have wrote the following code for it:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;stdlib.h&gt;
  int count_insensitive(char *str, char ch){
      int n = 0;
      int i;
      for (i=0;i&lt;strlen(str);i++){
          if (tolower(*(str+i))== tolower(ch)){
              n++;
          }
      }
      return n;
  }
  int main(){
      char *a,ch;
      int i,n;
      n=50;
      a = (char*) malloc(sizeof(char) * n);
      if (a==NULL){
          exit(1);
      }
      fgets(a,sizeof(a),stdin);
      char str[strlen(a)];
      strcpy(str,a);
      free(a);
      char c[] = {'b','H','8','u','$'};
      for (i=0;i&lt;5;i++){
          ch = c[i];
          printf("The character '%c' occurs %d times.\n",c[i],count_insensitive(str,ch));
      }
      return 0;
  }
  </code></pre>
  
  <p>The program runs  but it take only the first 3 characters of a string and prints the position. Can you help me what I am doing wrong.</p>