Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<ctype.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6. int cmp(const void *p1, const void *p2);
  7.  
  8. int main() {
  9. char **ans = NULL, *buffer = NULL;
  10. int ch, currentSize = 0, size = 0;
  11. buffer = malloc(1);
  12. if (buffer == NULL) {
  13. perror("");
  14. exit(0);
  15. }
  16. while ((ch = getchar()) != EOF) {
  17. if (isspace(ch)) {
  18. if (currentSize != 0) {
  19. ans = realloc(ans, (size + 1) * sizeof(char*));
  20. if (ans == NULL) {
  21. perror("");
  22. exit(0);
  23. } else {
  24. buffer = realloc(buffer, (currentSize + 1) * sizeof(char));
  25. buffer[currentSize] = '\0';
  26. ans[size++] = buffer;
  27. buffer = malloc(1);
  28. if (buffer == NULL) {
  29. perror("");
  30. exit(0);
  31. }
  32. currentSize = 0;
  33. }
  34. }
  35. }
  36. else {
  37. buffer = realloc(buffer, currentSize + 1);
  38. if (buffer == NULL) {
  39. perror("");
  40. exit(0);
  41. } else {
  42. buffer[currentSize++] = ch;
  43. }
  44. }
  45. }
  46. if (currentSize != 0) {
  47. ans = realloc(ans, (size + 1) * sizeof(char*));
  48. if (ans == NULL) {
  49. perror("");
  50. exit(0);
  51. }
  52. else {
  53. buffer[currentSize] = '\0';
  54. ans[size++] = buffer;
  55. }
  56. } else {
  57. free(buffer);
  58. }
  59.  
  60. qsort(ans, size, sizeof(char*), cmp);
  61. int i;
  62. for (i = 0; i < size; ++i) {
  63. printf("\"%s\"\n", ans[i]);
  64. free(ans[i]);
  65. }
  66. return 0;
  67. }
  68.  
  69. int cmp(const void *p1, const void *p2) {
  70. return strcmp(* (char * const *) p1, * (char * const *) p2);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement