Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ============================================================================
- Name : takeHomeFinal.c
- Author :
- Version :
- Copyright : Your copyright notice
- Description : Hello World in C, Ansi-style
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_WORD_LENGTH 32
- int countWord(FILE *in_file, char *word) {
- int count, ind = 0;
- char line[999];
- while (fgets(line, 999, in_file) != NULL) { // read a line
- char *occur = line;
- while((occur = (strstr(occur, word)))!= NULL){
- count++;
- ++occur;
- }
- }
- //Close File
- fclose(in_file);
- //return count if non-zero
- if (count != 0) {
- printf("The word \"%s\" was found %d times in the US Constitution\n",
- word, count);
- // return the count
- return count;
- }
- else {
- printf("The word \"%s\" was not found in the US Constitution", word);
- return 0;
- }
- return 0;
- }
- int main() {
- printf("Joseph Bensen, UID: 114670540\n");
- FILE *in_file = fopen("The_US_Constitution.txt", "r"); //Open the input file here
- char word[32];
- printf("Enter the word you would like to find in the US Constitution: ");
- scanf("%s", word);
- while (strlen(word) < 5 || strlen(word) > 32) {
- printf(
- "\nIncorrect word length entered, please enter a different word: ");
- scanf("%s", word);
- }
- /* Try to open file */
- in_file = fopen("The_US_Constitution.txt", "r"); //OPEN THE input file here
- /* Exit if file not opened successfully */
- if (in_file == NULL) {
- printf("\nUnable to open file.\n");
- exit(EXIT_FAILURE);
- }
- return countWord(in_file, word);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement