Question: #include #include #include libfindstr.h #define MAXCHAR 1000 #define FILENAME infile.txt int main() { /* * length of the string read from text file * and

#include
#define MAXCHAR 1000 #define FILENAME "infile.txt"
int main() { /* * length of the string read from text file * and string taken from input */ int lenTxt, lenIn;
/* * sub-string location initialized with negative values. * So if the sub-string is found from the text file, * the locations become non-negative. */ int lineLoc = -1, colLoc = -1; char strIn[MAXCHAR], strTxt[MAXCHAR], c; FILE *fp;
fp = fopen(FILENAME, "r");
if (fp == NULL) { printf("Opening file %s failed. ", FILENAME); return 0; } else{ /*read string to strTxt[] from text file*/ readstrtxt(strTxt, fp);
/*take sub-string to be searched from input*/ printf("Input a string to be searched for: "); readstrinput(strIn);
lenTxt = strlen(strTxt); lenIn = strlen(strIn); if (lenTxt > 0 && lenIn > 0) { if (strTxt[lenTxt] == '\0') printf("strTxt is null terminated "); if (strIn[lenIn] == '\0') printf("strIn is null terminated "); findstrloc(strTxt, strIn, &lineLoc, &colLoc); if (lineLoc >= 0) printf("The first occurrence of string \"%s\" is at line %d column %d. ", strIn, lineLoc, colLoc); else printf("The string \"%s\" is not in the text file. ", strIn); } fclose(fp); } return 0;
}
(Please test the output)
Q3) In this problem, you will write three functions a function readstrtxt that reads every character in a text file into a single one-dimensional character array; a function readstrinput that reads a line from standard input into a character array (for the purposes of this problem a line of text is a sequence of characters up to, but not including, a newline character); and a function fi that checks if a sub-string is present in a larger string. You must use the following function prototypes: void readstrtxt (char str 0, FILE *fp); void read strinput char substr D]); void findstrloc (char str[J, char substr[], int *linePtr, i nt *colPtr) The function findstrloc takes as input a string (str), a sub-string (substr and two integer pointers. The pointers linePtr and colPtr point to integer variables in the caller. After calling findstrloc, the caller's integer variables should be updated with the line and column indices of the first occurrence of the sub-string in the larger string. If the sub-string does not appear in the larger string, then the line and column indices should be set to -1 before findstrloc returns Example input/output pairs (excluding the prompt) are provided below (a) Input: 1973; Output: The first occurrence of string "1973" is at line 0 column 85. (b) Input: C++; Output: The first occurrence of string "C++" is at line 1 column 11 (c) Input: Brian; Output: The string "Brian" is not in the text file. Hint: In C, 'In' is used to create a new line. To determine the current line index, you will have to count the number of 'In' characters that have been encountered in the large string. Note: You must write your function prototypes in a header file named libfindstr.h and you must write your function definitions in the source file named libfindstr.c. We are providing you the main source file findstr.c and a text file infile.txt, which you can use to test your functions. Do not change anything in findstr.c
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
