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

You are to use predefined C string functions. Assume you have the following stri

ID: 3807036 • Letter: Y

Question

You are to use predefined C string functions. Assume you have the following strings defined: char strl = "this is some text and some more", buf2[100]; char *ptrl, *ptr2; (a) In one C statement, assign pointer ptrl to the first occurrence of the character e in strl. (b) In one C statement, assign pointer ptr2 to the last occurrence of the character e in strl. (c) In one C statement, copy the substring of strl from ptrl to ptr2 (inclusively) into str2. Don't worry about bounds checking or null terminating. (d) In one C statement, append the word "chicken" onto the end of strl. Again, don't worry about bounds checking.

Explanation / Answer

C Program

#include<stdio.h>
#include<string.h>
int main()
{
   char str1[] = "this is some text and some more", str2[100];
   char *ptr1,*ptr2;
   int k,first = 0,N = strlen(str1);
   for(k = 0;k<N;k++)
   {
       if (str1[k] == 'e')
       {
           if(first == 0) // checking first occurence of e or not
           {
               ptr1 = &str1[k];// if yes assign ptr1 as the pointer to that e
               first = 1;// and make first is happened
           }
           ptr2 = &str1[k];// during each occurence of e ptr2 will update and points to the lase e
       }
   }
   strncpy(str2,ptr1,(ptr2-ptr1+1));// copy the substring of str1 from ptr1 to ptr2
   printf("str2 = %s ",str2);// print the str2
   strcat(str1,"chicken");// append the word "chicken" to the end of the str1
   printf("str1 = %s ",str1);// print str1
   return 0;
}

OUTPUT

$ ./a.out
str2 = e text and some more
str1 = this is some text and some morechicken

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