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

Please help me write this code in C programming language, thank you! Write a pro

ID: 3804479 • Letter: P

Question

Please help me write this code in C programming language, thank you!

Write a program and functions that does not use strchr0, strrchr0, or memchr0 and will do the following: Main function a. Declare a character array of size 41. i. Remember to define a constant for the value 41 and use it in your program instead of typing 41. b. Has a loop that does the following actions until a blank line is entered. i. Prompt the user to enter a line of text. ii. Use fgets0 to read the line and store it in your character array. iii. If necessary, clear garbage out of the input buffer. iv. Prompt the user to enter a character to search for. v. Read in the character and clear any garbage from the input buffer. vi. Call function CharlsAt0 to find the location (s) of the character in the input line. vii. Print out the following information: 1. The line of text that was entered. 2. The character being searched for. 3. The number of locations at which the character was found. 4. The location of each occurrence of the character relative to the start of the line. int CharlsAt(char*pStr, char ch, int loc [], int mLoc) a. Returns an integer value giving the number of times ch was found in *pStr. b. Accepts the following arguments i. pStr: a pointer to the input string. ii. ch: character to search for. iii. loc: array for storing the locations at which the ch was found. iv. mLoc: the size of the array loc. c. The function searches for ch in *pStr by incrementing a pointer to access and compare each character in *pStr to ch. d. When it finds a match, it stores the index of the character's position in loc. Format your source code properly. Using meaningful variable names. Make sure that you do not allow any buffer overflows to occur! An example showing how to use pointers to a string to do a search is shown in listing 11.14 at the top of page 467 in our textbook.

Explanation / Answer

#include<stdio.h>
#define ARR_SIZE 41//constant for the size of the array

int CharIsAt(char *pStr,char ch,int loc[],int mLoc);//Function prototype declarations

int check_blank_line(const char *arr);//Checking whether blank line entered or not

void clear_stdin();//Clearing input buffer


int main()
{
   char pStr[ARR_SIZE],ch;//declaring character array of size 41 and character variable to store the character to search for in the line
   int loc[ARR_SIZE],num_occ,i;//declaring integer array of size 41 to store occurances of character.
   do
   {
       printf("Enter line of text... ");
       fgets(pStr,ARR_SIZE,stdin);
       if(check_blank_line(pStr))
       {
           printf("Blank line is entered... exiting... ");
           break;
       }

      
       clear_stdin();//clearing the buffer incase user enters more than 40 characters
  
       printf("Enter the character to search for... ");
       scanf("%c",&ch);
       clear_stdin();
       num_occ = CharIsAt(pStr,ch,loc,ARR_SIZE);
       printf("Line of text entered is: %s ",pStr);
       printf("Character being searched for: %c ",ch);
       printf("Number of locations it was found: %d ",num_occ);
       printf("Location of occurances relative to the start of the line.. ");
       for(i=0;i<num_occ;i++)
           printf("%d ",loc[i]);
       printf(" ");
  

   }while(1);
  
   return 0;  


}

int CharIsAt(char *pStr,char ch,int loc[],int mLoc)
{
   int pStr_len = 0,i=0,j=0;
   for(;pStr[pStr_len];pStr_len++);//Calculating the size of the line of text
   for( ;i<pStr_len && j<mLoc; i++)//Iterate until end of the line of text and also checking whether loc buffer size is exceeded
   {
       if(pStr[i] == ch)//Comparing character at ith position of the line of text
       {
           loc[j] = i;//storing the location of the occurance
           j++;//incrementing the index of loc buffer to store next occurance
       }

   }
   return j;//returning number of occurances of the character

}


int check_blank_line(const char *arr)
{
  
   if( arr[0]==' '&& arr[1]=='')//Checking whether blank line is entered or not
       return 1;
   else
       return 0;

}

void clear_stdin()//Clearing input buffer
{
   int ch;

   while( (ch=getchar())!=EOF && ch!=' ' );//clearing the input buffer

}

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