Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.  
  7. const int m1 = 10, n1 = 20, N = 2;
  8.  
  9. int found(char **book, char *word, int *count, int n) {
  10. int i, j, f;
  11. for(i = 0, f = 1; (i < n) && (f); i++)
  12. if(strcmp(word, book[i]) == 0) {
  13. f = 0;
  14. count[i]++;
  15. }
  16. if(f) {
  17. strcpy(book[n], word);
  18. n++;
  19. }
  20. return n;
  21. }
  22.  
  23. int input(char str[], int n1) {
  24. int c, i = 0;
  25. c = getchar();
  26. while((c != EOF) && (c != '\n')) {
  27. if(i > strlen(str))
  28. realloc(str, n1 * sizeof(char));
  29. str[i] = (char) c;
  30. c = getchar();
  31. i++;
  32. }
  33. if(c == '\n')
  34. return 1;
  35. else
  36. return 0;
  37. }
  38.  
  39. int main() {
  40. char **book, *str, *word; int *count, i, j, n = 0;
  41. book = (char **) malloc(m1 * sizeof(char));
  42. for(i = 0; i < m1; i++)
  43. book[i] = (char *) malloc((n1 + 1) * sizeof(char));
  44. str = (char *) malloc((n1 + 1) * sizeof(char));
  45. word = (char *) malloc((n1 + 1) * sizeof(char));
  46. count = (int *) malloc(m1 * sizeof(char));
  47. for(i = 0; i < m1; i++)
  48. count[i] = 0;
  49. while(input(str, n1)) {
  50. for(i = 0, j = 0; str[i] != '\0'; i++)
  51. if((str[i] == ' ') || (str[i] == '\n')) {
  52. word[j] = '\0';
  53. n = found(book, word, count, n);
  54. j = 0;
  55. }
  56. else {
  57. word[j] = str[j];
  58. j++;
  59. }
  60. }
  61. for(i = 0; i < n; i++)
  62. if(count[i] >= N)
  63. fputs(book[i], stdout);
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement