Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1 // Lukas Lipp
- 2 // tea = text einlesen / ausgabe
- 3 //
- 4 //
- 5 #include <stdlib.h>
- 6 #include <stdio.h>
- 7 #define BLOCKSIZE 10
- 8
- 9 int resize_memory(char** oldpointer, int newsize) // allocates new memory or frees memory, depending on the new size
- 10 {
- 11 char* newpointer = realloc(*oldpointer, newsize);
- 12 if(newpointer == NULL)
- 13 {
- 14 free(*oldpointer);
- 15 return -1;
- 16 }
- 17 *oldpointer = newpointer;
- 18 return 0;
- 19 }
- 20
- 21 int main(int argc, const char *argv[])
- 22 {
- 23 char* input = malloc(sizeof(char) * BLOCKSIZE);
- 24
- 25 int allocatedblocks = 1;
- 26 int readchars = 0;
- 27 char buffer;
- 28
- 29 buffer = getchar();
- 30 while(buffer != EOF) // reads input until EOF
- 31 {
- 32 if(readchars >= BLOCKSIZE*allocatedblocks) // allocates new memory if there is currently no memory left for further input
- 33 {
- 34 if(resize_memory(&input, sizeof(char) * (BLOCKSIZE * (allocatedblocks+1))) == -1) // allocates memory for one more BLOCK of input characters
- 35 return -1;
- 36 allocatedblocks++;
- 37 }
- 38 input[readchars] = buffer;
- 39 buffer = getchar();
- 40 readchars++;
- 41 }
- 42
- 43 if(resize_memory(&input, sizeof(char) * readchars) == -1)
- 44 return -1;
- 45
- 46 int character[25];
- 47 int counter = 0;
- 48
- 49 for(counter = 0; counter < readchars; counter++)
- 50 {
- 51 character[input[counter]-'a']++;
- 52 }
- 53
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement