Advertisement
Guest User

Untitled

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