title: Reduce amount of C code
body: <p>How can I refactor this with less code?
  This is homework and is cracking a Caesar cipher-text using frequency distribution.</p>
  
  <p>I have completed the assignment but would like it to be cleaner.</p>
  
  <pre><code>int main(int argc, char **argv){
  
  // first allocate some space for our input text (we will read from stdin).
  char* text = (char*)malloc(sizeof(char)*TEXT_SIZE+1);
  char textfreq[ALEN][2];
  char map[ALEN][2];
  char newtext[TEXT_SIZE];
  char ch, opt, tmpc, tmpc2;
  int i, j, tmpi;
  
  // Check the CLI arguments and extract the mode: interactive or dump and store in opt.
  if(!(argc == 2 &amp;&amp; isalpha(opt = argv[1][1]) &amp;&amp; (opt == 'i' || opt == 'd'))){
      printf("format is: '%s' [-d|-i]\n", argv[0]);
      exit(1);
  }
  
  // Now read TEXT_SIZE or feof worth of characters (whichever is smaller) and convert to uppercase as we do it.
  
  for(i = 0, ch = fgetc(stdin); i &lt; TEXT_SIZE &amp;&amp; !feof(stdin); i++, ch = fgetc(stdin)){
      text[i] = (isalpha(ch)?upcase(ch):ch);
  }
  text[i] = '\0'; // terminate the string properly.
  
  // Assign alphabet to one dimension of text frequency array and a counter to the other dimension
  
  for (i = 0; i &lt; ALEN; i++) {
      textfreq[i][0] = ALPHABET[i];
      textfreq[i][1] = 0;
  }
  
  // Count frequency of characters in the given text
  for (i = 0; i &lt; strlen(text); i++) {
      for (j = 0; j &lt; ALEN; j++) {
          if (text[i] == textfreq[j][0]) textfreq[j][1]+=1;
      }
  }
  
  //Sort the character frequency array in descending order
  for (i = 0; i &lt; ALEN-1; i++) {
      for (j= 0; j &lt; ALEN-i-1; j++) {
          if (textfreq[j][1] &lt; textfreq[j+1][1]) {
              tmpi = textfreq[j][1];
              tmpc = textfreq[j][0];
              textfreq[j][1] = textfreq[j+1][1];
              textfreq[j][0] = textfreq[j+1][0];
              textfreq[j+1][1] = tmpi;
              textfreq[j+1][0] = tmpc;
          }
      }
  }
  
  //Map characters to most occurring English characters
  for (i = 0; i &lt; ALEN; i++) {
      map[i][0] = CHFREQ[i];
      map[i][1] = textfreq[i][0];
  }
  
  // Sort the map lexicographically
  for (i = 0; i &lt; ALEN-1; i++) {
      for (j= 0; j &lt; ALEN-i-1; j++) {
          if (map[j][0] &gt; map[j+1][0]) {
              tmpc = map[j][0];
              tmpc2 = map[j][1];
              map[j][0] = map[j+1][0];
              map[j][1] = map[j+1][1];
              map[j+1][0] = tmpc;
              map[j+1][1] = tmpc2;
          }
      }
  }
  
  if(opt == 'd'){
      decode_text(text, newtext, map);
  } else {
  // do option -i
  }
  
  // Print alphabet and map to stderr and the decoded text to stdout
  fprintf(stderr, "\n%s\n", ALPHABET);
  for (i = 0; i &lt; ALEN; i++) {
      fprintf(stderr, "%c", map[i][1]);
  }
  printf("\n%s\n", newtext);
  return 0;
  }
  </code></pre>