Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 22nd, 2012  |  syntax: None  |  size: 0.96 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "letter_count.h"
  5.  
  6. int main (int argc, char const* argv[])
  7. {
  8.         int i;
  9.         for (i=1;i<argc; i++){
  10.                 letter_distribution_of(argv[i]);
  11.         }
  12.         return 0;
  13. }
  14.  
  15. void letter_distribution_of(const char *value){
  16.         int i;
  17.         int *letter_counts = (int *) malloc(2 * sizeof(int));
  18.         int current_length=2;
  19.         for(i=0;i<strlen(value);i++){
  20.                 int j;
  21.                 int changed = 0;
  22.                 for (j=0;j<current_length-2;j+=2){
  23.                         if(value[i] == letter_counts[j]){
  24.                                 letter_counts[j+1]++;
  25.                                 changed = 1;
  26.                         }
  27.                 }
  28.                 if(changed == 0){
  29.                         letter_counts[current_length-2] = value[i];
  30.                         letter_counts[current_length-1] = 1;
  31.                         if(i < strlen(value)-2)
  32.                         current_length += 2;
  33.                         letter_counts = (int *) realloc(letter_counts, current_length * sizeof(int));
  34.                 }
  35.         }
  36.         printf("Letter Distribution of %s:\n", value);
  37.         for(i=0;i<current_length;i+=2){
  38.                 printf("%c:%d\t",letter_counts[i], letter_counts[i+1]);
  39.         }
  40.         free(letter_counts);
  41.         puts(""); //newline for formatting.
  42. }