Skip to content

Instantly share code, notes, and snippets.

@ballpointcarrot
Created October 8, 2011 16:56
Show Gist options
  • Save ballpointcarrot/1272549 to your computer and use it in GitHub Desktop.
Save ballpointcarrot/1272549 to your computer and use it in GitHub Desktop.
Letter Distribution Calculator
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "letter_count.h"
int main (int argc, char const* argv[])
{
int i;
for (i=1;i<argc; i++){
letter_distribution_of(argv[i]);
}
return 0;
}
void letter_distribution_of(const char *value){
int i;
int *letter_counts = (int *) malloc(2 * sizeof(int));
int current_length=2;
for(i=0;i<strlen(value);i++){
int j;
int changed = 0;
for (j=0;j<current_length-2;j+=2){
if(value[i] == letter_counts[j]){
letter_counts[j+1]++;
changed = 1;
}
}
if(changed == 0){
letter_counts[current_length-2] = value[i];
letter_counts[current_length-1] = 1;
if(i < strlen(value)-2)
current_length += 2;
letter_counts = (int *) realloc(letter_counts, current_length * sizeof(int));
}
}
printf("Letter Distribution of %s:\n", value);
for(i=0;i<current_length;i+=2){
printf("%c:%d\t",letter_counts[i], letter_counts[i+1]);
}
free(letter_counts);
puts(""); //newline for formatting.
}
#ifndef LETTER_COUNT_H
#define LETTER_COUNT_H
/* function declarations */
void letter_distribution_of(const char *value);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment