#include #include struct ahel { char *str; struct ahel *next; } ; struct ahel *getahel(void); int *statistics(struct ahel*); void printstat(int*); int main() { struct ahel *loend; int *counts, exxit = 0; char input[81]; while(!exxit) { printf("Sisestage palun string:\n"); loend = getahel(); counts = statistics(loend); printstat(counts); printf("\nKorrata(j/e): "); fgets(input, 81, stdin); if(input[0] == 'e' || input[0] == 'E') return 0; } return 0; } // buffer - tehakse malloc. hoiduda leketest. struct ahel *getahel(void) { struct ahel *algus, *moment, *minevik, *abi; char input[81], *buffer; moment = algus = malloc(sizeof(struct ahel)); minevik = abi = malloc(sizeof(struct ahel)); abi->str = malloc(1); abi->str[0] = 0; while(minevik->str[0] != '\n') { input[0] = ' '; input[1] = 0; moment->str = malloc(1); moment->str[0] = 0; while(input[strlen(input)-1] != '\n') { fgets(input, 81, stdin); moment->str = realloc(moment->str, strlen(moment->str) + strlen(input) + 1); strcat(moment->str, input); } minevik = moment; moment = malloc(sizeof(struct ahel)); minevik->next = moment; } // viimane sai ilmaasjata eraldatud. free(moment); free(abi->str); free(abi); minevik->next = NULL; return algus; } int *statistics(struct ahel *loend) { int i, *counts; struct ahel *ptr; counts = malloc(26); memset(counts, 0, 26 * sizeof(int)); while(loend != NULL) { for(i=0; loend->str[i]!=0; i++) { if(loend->str[i] > 90) loend->str[i] -= 32; if(loend->str[i] > 64 && loend->str[i] < 91) counts[loend->str[i]-65]++; } ptr = loend; loend = loend->next; free(ptr->str); free(ptr); } return counts; } void printstat(int *counts) { int i; for(i=65; i<91; i++) printf(" %c", (char) i); putchar('\n'); for(i=0; i<26; i++) printf("%3i", counts[i]); putchar('\n'); }