write program in C language, the first good answer will be rated. Please find th
ID: 3825481 • Letter: W
Question
write program in C language, the first good answer will be rated. Please find the contents of the file decl. of indep.txt below (copy as is):
Program 2 -Detecting Substrings Introduction A very common task that is often performed by programs that work with text files is the problem of locating a specific substring within the file. I'm sure we've all done this many times in working with Word, Notepad, or other editors. Since we don't have a GUI or other means of displaying the contents of a file all at once, let's modify the problem slightly. Rather than locating a specific substring within a file and then highlighting the results, as most modern programs do, let's write a C program that locates the occurrences of a specific substring within a file and then display the occurrence number as well as a portion of the text around the found substring. It should also count the number of occurrences and indicate that in a brief report at the end. Although it should work on any input file and any substring, we'll provide a copy of an input file to use for testing (i.e "Decl of Indep txt"). Overview Here's a high level overview of what your program should do: 1) Ask the user for the name of the input file a) If the file is not present or can't be opened, display an error message and ask the user to enter another file name. 2) Ask the user for the substring to search for 3) Calculate and display the found occurrences of the substring in the file. For each found occurrence, your program should display (a) the location number starting at 1 and going up to the total number of locations found, and (b) the portion of the string containing the found substring. This portion should consist of the substring and 8 characters before and 8 characters after the found substring.Explanation / Answer
#include <stdio.h>
#include <string.h>
FILE *fr;
void readFile(){
int isFileValid = 0;
char fileName[50] = {''};
while(isFileValid != 1 ){
printf("Enter input file name : ");
scanf("%s",&fileName);
fr = fopen (fileName,"rt");
if(fr == NULL ){
printf("%s File not found. ",fileName);
}else{
isFileValid = 1;
}
}
int total = 0;
char line[50] = {''};
char stringToBeSearched[50] = {''};
printf("Enter string which need to be searhced");
scanf("%s",&stringToBeSearched);
printf("Entered search string %s ",stringToBeSearched);
while ( fgets ( line, sizeof line, fr ) != NULL )
{
char *pos;
if ((pos=strchr(line, ' ')) != NULL){
*pos = '';
}
printf("%s ",line);
char *p = line;
while (strstr(p,stringToBeSearched) != NULL )
{
if(p){
total++;
printf ("First occurrence of string "test" in "%s" is"
" "%s" ",line, p);
}else{
printf("string not found " );
}
p++;
}
printf("%d Total occurences ",total);
}
}
int main (){
readFile();
return 0;
}
output:
---------------
29 Total occurences you too good
First occurrence of string "test" in "you too good" is "you too good"
First occurrence of string "test" in "you too good" is "ou too good"
First occurrence of string "test" in "you too good" is "u too good"
First occurrence of string "test" in "you too good" is " too good"
First occurrence of string "test" in "you too good" is "too good"
First occurrence of string "test" in "you too good" is "oo good"
First occurrence of string "test" in "you too good" is "o good"
First occurrence of string "test" in "you too good" is " good"
First occurrence of string "test" in "you too good" is "good"
First occurrence of string "test" in "you too good" is "ood"
First occurrence of string "test" in "you too good" is "od"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.