Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. Name : takeHomeFinal.c
  4. Author :
  5. Version :
  6. Copyright : Your copyright notice
  7. Description : Hello World in C, Ansi-style
  8. ============================================================================
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #define MAX_WORD_LENGTH 32
  15.  
  16. int countWord(FILE *in_file, char *word) {
  17. int count, ind = 0;
  18. char line[999];
  19.  
  20. while (fgets(line, 999, in_file) != NULL) { // read a line
  21. char *occur = line;
  22. while((occur = (strstr(occur, word)))!= NULL){
  23. count++;
  24. ++occur;
  25. }
  26. }
  27.  
  28. //Close File
  29. fclose(in_file);
  30. //return count if non-zero
  31. if (count != 0) {
  32. printf("The word \"%s\" was found %d times in the US Constitution\n",
  33. word, count);
  34. // return the count
  35. return count;
  36. }
  37. else {
  38. printf("The word \"%s\" was not found in the US Constitution", word);
  39. return 0;
  40. }
  41. return 0;
  42. }
  43.  
  44. int main() {
  45. printf("Joseph Bensen, UID: 114670540\n");
  46. FILE *in_file = fopen("The_US_Constitution.txt", "r"); //Open the input file here
  47. char word[32];
  48. printf("Enter the word you would like to find in the US Constitution: ");
  49. scanf("%s", word);
  50. while (strlen(word) < 5 || strlen(word) > 32) {
  51. printf(
  52. "\nIncorrect word length entered, please enter a different word: ");
  53. scanf("%s", word);
  54. }
  55.  
  56. /* Try to open file */
  57. in_file = fopen("The_US_Constitution.txt", "r"); //OPEN THE input file here
  58.  
  59. /* Exit if file not opened successfully */
  60. if (in_file == NULL) {
  61. printf("\nUnable to open file.\n");
  62. exit(EXIT_FAILURE);
  63. }
  64. return countWord(in_file, word);
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement