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

Use C (Pls specify which is libfindstr.h and libfindstr.c) Q3) In this problem,

ID: 3861559 • Letter: U

Question

Use C (Pls specify which is libfindstr.h and libfindstr.c)

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 findstrloc that checks if a sub-string is present in a larger string. You must use the following function prototypes:

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, ’ ’ is used to create a new line. To determine the current line index, you will

have to count the number of ’ ’ 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.

(findstr.c)

#include <stdio.h>
#include <string.h>
#include "libfindstr.h"

#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] == '')
               printf("strTxt is null terminated ");
           if (strIn[lenIn] == '')
               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;
}

(infile.txt)

C is a computer programming language developed by Dennis M. Ritchie between 1969 and 1973 at Bell Labs.
Both C and C++ will be covered in this course.
To become an excellent programmer, the only way is to practice.

Explanation / Answer

Here is the code for you:

//reads every character in a text file into a single one-dimensional character array
void readstrtxt(char str[], FILE *fp)
{
    int i = 0;
    while(!feof(fp))
        str[i++] = fgetc(fp);
}
//reads a line from standard input into a character array
void readstrinput(char subStr[])
{
    int i = 0;
    /*while(!feof(stdin))
        subStr[i++] = fgetc(stdin);*/
    size_t bufSize = 256;
    getline(&subStr, &bufSize, stdin);  
}
//checks if a sub-string is present in a larger string.
void findstrloc(char str[], char subStr[], int *linePtr, int *colPtr)
{
    int i = 0, j = 0, flag;
    int strLen = strlen(str);
    int substrLen = strlen(subStr);
    *linePtr = 0;
    *colPtr = 0;
    for (i = 0; i <= strLen - substrLen; i++)
{
for (j = i; j < i + substrLen; j++)
{
if(str[i] == ' ')
   {
       (*linePtr)++;
       *(colPtr) = 0;
   }      
flag = 1;
(*colPtr)++;
if (str[j] != subStr[j + i])
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
  
}

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