Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that counts the number of occurrences of a substring within a fi

ID: 3815949 • Letter: W

Question

Write a program that counts the number of occurrences of a substring within a file. The program must allow the user to enter both the substring the user wants to search for and the name of the file to be searched. The output message must include the number of occurrences of the substring, the substring, and the name of the file searched.

Your program will perform a case sensitive search. If the search string specified by the user is “the”, “THE” and “The” will not be counted.

Be sure to consider the possibility of overlapping substrings. For example: if the user specified a file containing the string “boobooboo” and the substring to search for is “boo”, the count would be 3. However, if the substring to search for is “booboo”, the count would be 2 (the first occurrence starts with the first character of the string, the second occurrence starts with the 4th character of the string.

You may assume that the lines of text will not be longer than 250 characters.

Several sample input files are included with this assignment. Do not assume that these files can be used to completely test your program. Your program should work for any text file and any substring. An input file can contain any printable character. You should create various test files to gain confidence that your program works correctly.

This is not an array problem. It is a file and string problem. It is not possible to store all the strings read from the file in some huge two-dimensional array. The file will likely contain too many strings for this.

Do not go searching in the library for other string functions. You may only use the functions discussed in Chapters 1-9 of the text or lectures. IF YOU USE OTHER LIBRARY FUNCTIONS YOU WILL NOT RECEIVE CREDIT FOR THIS EXERCISE.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LEN 20
int main(int argc, char* argv[])
{
   FILE* fp=NULL;
   char str[MAX_LEN];
   char substr[MAX_LEN];
   unsigned int count = 0;
   unsigned int i = 0;
   unsigned int len = 0;

   if (argc != 3) {
       printf(" arguments error...");
       exit(1);
   }
   fp = fopen(argv[1],"r");


   if (NULL == fp){
       printf(" Error in reading file..");
       exit(1);
   }
   strcpy(substr,argv[2]);

   while (fscanf(fp,"%s",str) != EOF) {
       //printf(" %s",str);
       for(i=0;i<strlen(str);i++) {
           len = strlen(substr);
           if (strncmp(str+i,substr,len) == 0) {
               count++;
           }
       }  
   }
   printf(" No of occurances of string |%s| in file are |%d|",substr,count);
   return 0;
}

This program restricts the size of the substring to be seached and size of each word in file to 20.you can easily change this by updating MAX_LEN.

Please let me know in case of any issues.

thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote