title: How to compress a ppm image in C
body: <p>for a exercice i must compress a ppm image without including a library that does it for us.
  For example before compression:</p>
  <pre><code>6
  6
  6
  2
  2
  </code></pre>
  <p>After compression :</p>
  <pre><code>6: 3
  2: 2
  </code></pre>
  <p>I made a Array but I don't know how to do the compression algorithm</p>
  <pre><code>int get_nb_lin(char *str)
  {
      int nbr = 0;
  
      for (int i = 0; str[i] != '\0'; ++i) {
          if (str[i] == '\n')
              nbr++;
      }
      return (nbr);
  }
  
  int get_len_lin(char *str)
  {
      int nbr = 0;
  
      for (int i = 0; str[i] != '\n'; ++i) {
          if (str[i] == '\0')
              return (nbr);
          nbr++;
      }
      return (nbr);
  }
  
  char **buf_to_arr(char *str)
  {
      int nb = get_nb_lin(str);
      int bn = get_len_lin(str);
      char **tab = malloc(sizeof(char*) * (nb + 2));
      int k = 0;
      int l = 0;
  
      tab[k] = malloc(sizeof(char) * (bn + 2));
      for (int i = 0; str[i] != '\0'; i++) {
          if (str[i] == '\n') {
              tab[k][l] = '\0';
              (str[i + 1] != '\0') ? k++,
              tab[k] = malloc(sizeof(char) * (bn + 2)), l = 0 : 0;
          }
          tab[k][l] = str[i];
          l = (tab[k][l] == '\n') ? l - 1 : l;
          l++;
      }
      tab[k][l] = '\0';
      tab[k + 1] = NULL;
      return (tab);
  }
  </code></pre>
  <p>Thanks for your help !</p>