Advertisement
I_LIKE_COFFEE

search alfa

Jan 20th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define SIZE 80
  5.  
  6.  
  7. int main() {
  8. FILE* file_ptr;
  9. file_ptr = fopen("book.txt", "r");
  10.  
  11. char Harry[] = "harry";
  12. int len_h = strlen(Harry);
  13. char Potter[] = "potter";
  14. int len_p = strlen(Potter);
  15. char Harry_Potter[] = "harry potter";
  16. int len_hp = strlen(Harry_Potter);
  17. char boy_who_lived[] = "boy who lived";
  18. int len_bwl = strlen(boy_who_lived);
  19.  
  20. int c_Harry = 0;
  21. int c_Potter = 0;
  22. int c_Harry_Potter = 0;
  23. int c_boy_who_lived = 0;
  24.  
  25. char str[SIZE];
  26.  
  27.  
  28. if (file_ptr != NULL) {
  29. printf("File was found\n");
  30. while (fgets(str, SIZE, file_ptr) != NULL) {
  31. int i = 0, j = 0;
  32. int len_str = strlen(str);
  33. _strlwr(str);
  34.  
  35. while ((i <= len_str - len_h) & (j < len_h) || (i <= len_str - len_p) & (j < len_p) || (i <= len_str - len_bwl) & (j < len_bwl)){
  36. if (str[i + j] == Harry[j]) {
  37. j++;
  38. if (j == len_h) {
  39. c_Harry++;
  40. i++;
  41. j = 0;
  42. }
  43. }
  44. else if (str[i + j] == Potter[j]) {
  45. j++;
  46. if (j == len_p) {
  47. c_Potter++;
  48. i++;
  49. j = 0;
  50. }
  51. }
  52. else if (str[i + j] == boy_who_lived[j]) {
  53. j++;
  54. if (j == len_bwl) {
  55. c_boy_who_lived++;
  56. i++;
  57. j = 0;
  58. }
  59. }
  60. else {
  61. i++;
  62. j = 0;
  63. }
  64. }
  65. }
  66.  
  67. }
  68. else {
  69. printf("file not found");
  70. }
  71.  
  72. printf("Harry: %d\n", c_Harry);
  73. printf("Potter: %d\n", c_Potter);
  74. printf("Boy who lived: %d\n", c_boy_who_lived);
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement