Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define true 1
  6. #define false 0
  7.  
  8. int validateWord(char* str)
  9. {
  10. int lettersCounter = strlen(str);
  11. if(((str[0] >= 'a') && (str[0] <= 'z')) || ((str[0] >= 'A') && (str[0] <= 'Z')))
  12. {
  13. return true;
  14. }
  15. else if(str[1] == '\0')
  16. {
  17. return false;
  18. }
  19. else if(((str[lettersCounter/2] >= 'a') && (str[lettersCounter/2] <= 'z')) || ((str[lettersCounter/2] >= 'A') && (str[lettersCounter/2] <= 'Z')))
  20. {
  21. return true;
  22. }
  23. return false;
  24. }
  25. int countWords(FILE *f)
  26. {
  27. int count = 0;
  28. char buff[255];
  29. while (fscanf(f, "%s", buff) != EOF)
  30. {
  31. if(validateWord(buff))
  32. count++;
  33. }
  34. return count;
  35. }
  36. void ednakvi(FILE *fp, int wordCounter)
  37. {
  38. rewind(fp);
  39. int i = 0,k = 0,cnt = 1;
  40. char *arr[wordCounter];
  41. for (i=0; i<wordCounter; i++)
  42. arr[i] = (char *)malloc(255);
  43. i = 0;
  44. while(fscanf(fp, "%s", arr[i]) != EOF)
  45. {
  46. if(validateWord(arr[i]))
  47. {
  48. i++;
  49. }
  50. }
  51. for(i=0;i < wordCounter;i++)
  52. {
  53. if(arr[i][0] != '-')
  54. {
  55. for(k = i+1;k < wordCounter;k++)
  56. {
  57. if(!strcmp(arr[i],arr[k]))
  58. {
  59. arr[k][0] = '-';
  60. cnt++;
  61. }
  62. }
  63.  
  64. }
  65. else
  66. {
  67. cnt = 0;
  68. }
  69. if(cnt >= 1)
  70. {
  71. printf("%s : %d\n",arr[i],cnt);
  72. }
  73. cnt = 1;
  74. }
  75.  
  76. }
  77.  
  78.  
  79. int main(int agrc, char* argv[])
  80. {
  81. int wordCount = 0;
  82. FILE *rFile = fopen(argv[1], "r");
  83. wordCount = countWords(rFile);
  84. printf("%d\n", wordCount);
  85. ednakvi(rFile,wordCount);
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement