mcgizmo

Untitled

Dec 28th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. /* Write a program that calls from a list of names, */
  2. /* and print counts how many times each name appears. */
  3. /* The amount of file names not known in advance. */
  4. /* We can assume that each name appears on a separate line. */
  5.  
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8.  
  9. // function for count all of the words in the program
  10. int countWord(FILE *file)
  11. {
  12. int count = 0;
  13. char ch;
  14. // loop for counting words that shared by space
  15. while ((ch = fgetc(file)) != EOF)
  16. {
  17. if (ch == '\n')
  18. count++; // count all of the words in the program
  19. }
  20. return count;
  21. }
  22. // main() must return an integer
  23. void main() //settings function main ()
  24. {
  25. int wordCount = 0;
  26.  
  27. // open file it read-only mode
  28. FILE *file = fopen("d:\\names.txt", "r");
  29. //save the count words in the program in variable wordCount
  30. wordCount += countWord(file);
  31.  
  32. printf("We have %d words in the file\n", wordCount); // printg a counted words in file
  33. }
Add Comment
Please, Sign In to add comment