Write code to read in words from a given*word.txt* file, then prompt the user fo
ID: 3699363 • Letter: W
Question
Write code to read in words from a given*word.txt* file, then prompt the user for two words and print out how many words in the file fall between those words. If one of the two words is not contained in the file, then say word1 or word 2 is not found in the file.
Sample output:
Please type in the two words: good and night
good or night is not found.
Sample output:
Please type in the two words: hello and computer
There are 1234 words between hello and computer
Hint: you can use binarySearch method in slides of searching and sorting to figure out the index for the two words.
Explanation / Answer
#include #include int main(int argc, char const *argv[]) { int num =0; char word[2000]; char *string; FILE *in_file = fopen("words.txt", "r"); if (in_file == NULL) { printf("Error file missing "); exit(-1); } scanf("%s",word); printf("%s ", word); while(!feof(in_file))//this loop searches the for the current word { fscanf(in_file,"%s",string); if(!strcmp(string,word))//if match found increment num num++; } printf("we found the word %s in the file %d times ",word,num ); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.