Description: (In C language) You are to develop a program that will search for a
ID: 3677248 • Letter: D
Question
Description:
(In C language)
You are to develop a program that will search for a word as well as count the number of characters in a text file. Specifically, it will ask the user to input a file name (cstring) and a word (cstring). Then, it will decide whether the word exists in the file, and count the number of characters (A-Z lower or upper case alphabets, excluding all other symbols or special characters) in the file. After that, it will report the results: whether the word is contained in the file and how many characters are in the file.
Instructions:
Here are some basic components for your program design.
First, your main program gets the file name (string) from the keyboard input, make sure that the file exists while opening the file. Specifically, your program asks "Please input the text file name:" Then it uses scanf to read the string from the keyboard, which is the file name for fopen. If the file does not exist, which means that fopen will return NULL instead of the pointer to the file, your program notify the user and quit.
Second, if the file exists, the main program asks for a word "Please input the word to search:". After receive the word from the keyboard, it calls search_and_count() to search the word and count the characters in the file. So search_and_count() will have the file pointer and the word as input, and an indicator (0 or 1) (whether the word is contained in the file) and the total number of characters ('A'-'Z' or 'a'-'z' that are in the file) as output. Your main program then reports whether the word exists in the file, and how many characters are in the file.
search_and_count() will read a string from the file, count the characters in the string, compare the word with the string (to see if the word exists in the file), and repeat until the end of the file. Specifically, you will need to build two more functions:
· search_and_count() reads a string from the file into a variable, call function character_count() to count the number of characters in the string, and then call function word_search() to compare the word with the string to decide whether it is a match. This will go on in a loop until the end of file. If there is a match already, it doesn't need the comaprison any more. In other words, word_search() will be called only if your program hasn't found a match.
· character_count() should have an input, which is the current string, and a return value, which is the count of the number of characters in the string.
· word_search() should have two input, the word and the string, and a return value 0 or 1 as an indicator whether the word is the same as the string. The program should allow the string has an ending punctuation. That is, you may want to put a NULL in the string to make it the same length as the word, so that you can use strcmp to compare the word with the string.
Finally, in the main program, close your file and you report whether there is a match and how many characters are in the file.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORDS 5
#define MAX_WORD_LENGTH 100
#define MAX_SENTENCE_LENGTH 1000
int main(int argc, const char * argv[]) {
// initializing variables
int numWords = argc - 2;
int totalNumberOfWords = 0;
char * words[MAX_WORDS];
int counts[MAX_WORDS] = {0};
int coappearance[MAX_WORDS][MAX_WORDS] = {{0}, {0}};
char searchFileName[MAX_WORD_LENGTH];
FILE * searchFile;
// letting user know if they entered more than 5 words
if (numWords > 5) {
printf("Only 5 words maximum are allowed, you entered %d words ", numWords);
printf("Words starting with '%s' will be skipped ", argv[MAX_WORDS + 1]);
numWords = 5;
}
// adding search words to my array of words
for (int i = 0; i < numWords; i++) {
long wordLen = strlen(argv[i + 2]);
words[i] = malloc(wordLen + 1);
strcpy(words[i], argv[i + 2]);
}
// making sure enough words were entered
while (numWords < 2) {
printf("Minimum of 2 words are required. ");
printf("You entered %d words, please enter the next one: ", numWords);
words[numWords] = malloc(MAX_WORD_LENGTH);
scanf("%s", words[numWords]);
numWords++;
}
// Making sure file opened properly
strcpy(searchFileName, argv[1]);
searchFile = fopen(searchFileName, "r");
while (searchFile == NULL) {
printf("The file was not found, please enter a valid filename ");
scanf("%s", searchFileName);
searchFile = fopen(searchFileName, "r");
}
// printing all the search words in the words array for testing purposes
// for (int i=0; i<numWords; i++) {
// printf("word[%d] is '%s' ", i, words[i]);
// }
// Start reading in on file level until EOF
char currentWord[MAX_WORD_LENGTH];
char currentSentence[MAX_SENTENCE_LENGTH];
int wordLength = 0;
int sentenceLength = 0;
int wordsInSentence = 0;
int sharingWords[MAX_WORDS] = {0};
char c;
do {
c = getc(searchFile);
// Process word
if ((c == ',') || (c == ' ') || (c == '?') || (c == '!') || (c == '.') || (c == ' ')) {
if (wordLength > 0) {
totalNumberOfWords++;
currentWord[wordLength] = '';
// print word for testing purposes
// printf("Word # %3d is '%s' ", totalNumberOfWords, currentWord);
// compare the current word to the ones in word list
for (int i = 0; i < numWords; i++) {
if (strcasecmp(currentWord, words[i]) == 0) {
counts[i]++;
// if this word has not yet been listed for this sentence
if (sharingWords[i] == 0) {
sharingWords[i]++;
wordsInSentence++;
} else {}
} else {}
}
}
wordLength = 0;
} else {
wordLength++;
currentWord[wordLength - 1] = c;
}
// Process sentence
if (c != ' ') {
if ((c == '.') || (c == '?') || (c == '!')) {
currentSentence[sentenceLength] = '';
if (wordsInSentence > 1) {
char * trimmedSentence = currentSentence;
while (*trimmedSentence == ' ' || *trimmedSentence == ' ') {
trimmedSentence++;
}
printf("We have a match!: '%s' ", trimmedSentence);
for (int i = 0; i < MAX_WORDS; i++) {
if (sharingWords[i] == 1) {
for (int j = 0; j < MAX_WORDS; j++) {
if ((sharingWords[j] == 1) && (j != i)) {
coappearance[i][j]++;
} else {}
}
} else {}
}
} else {}
wordsInSentence = 0;
sentenceLength = 0;
for (int i = 0; i < MAX_WORDS; i++) {
sharingWords[i] = 0;
}
} else {
currentSentence[sentenceLength] = c;
sentenceLength++;
}
} else {}
} while (c != EOF);
printf(" The total number of words in file '%s' is %d. ", searchFileName, totalNumberOfWords);
for (int i = 0; i < numWords; i++) {
if (counts[i] != 0) {
printf("The number of times '%s' was found is %d. ", words[i], counts[i]);
} else {
printf("Word '%s' was not found in this file. ", words[i]);
}
for (int k = 0; k < numWords; k++) {
if ((k != i) && (coappearance[i][k] != 0)) {
if (coappearance[i][k] == 1) {
printf("Sharing sentence with '%s' once. ", words[k]);
} else {
printf("Sharing sentence with '%s' %d times. ", words[k], coappearance[i][k]);
}
} else {}
}
printf(" ");
}
// printf("End of file ");
// closing the search file
fclose(searchFile);
// freeing the memory allocated for words array
for (int i = 0; i < numWords; i++) {
free(words[i]);
}
return 0;
}