All pastes #126286 Raw Edit

ZaidaZadkiel

public c v1 · immutable
·published 2006-08-11 09:30 UTC
rendered paste body
/* Counts the times each character appears in input */#include <stdio.h>          /* stderr, stdout */#include <string.h>//Ignore anything non ascii.#define MAXARR 256char char_list[MAXARR];long char_count[MAXARR];int count(char *text){    long length = strlen(text);//    printf("text len:%d\n", length);    long c;    for(c=0; c!= length; c++){//	printf("[%d:%c]", c, text[c]);	char_list[text[c]]=text[c];	char_count[text[c]]++;    }}char filter(char c){    unsigned char r=c;    /* fall over, all these char codes turn into '?' */    switch(r){	case 0:        case 1:	case 2:	case 3:	case 4:	case 5:	case 6:	case 7:	case 8:	case 9:	case 10:	case 11:	case 12:	case 13:	case 14:	case 15:	case 142:	case 143:	    r='?';	    break;    }    return r;}int print(){    long length = 255;    long total = 0;    int c;    int printable;    for(c=0; c!= length; c++){	printable = filter(c);	fprintf(stdout, "%c: %d\t", printable, char_count[c]);	if(char_count[c] != 0) total++;    }        fprintf(stdout, "\n\ntotal: %d\n",total);}int main(int argc, char **argv) {    if(argc > 2 ){	fprintf(stderr, "Usage: %s [ filename ]\n", argv[0]);	return -1;    }        FILE* random;        if(argc == 2){	random = fopen(argv[1], "ro");	if(random == NULL){	    fprintf(stderr, "Error reading file '%s'\n", argv[1]);	    return -1;	}	        }else{	random = stdin;    } fprintf(stderr, "Error reading file '%s'\n", argv[1]);    /* clean arrays */    memset(char_list, 0, sizeof(char)*MAXARR);    memset(char_count, 0, sizeof(char)*MAXARR);        //4 bytes read from file    char text[1025];    while(fgets(text, 1024, random) != NULL){	count(text);        //fprintf(stdout, "%s", text);    }    print();    return 0;}