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

Answer where it says \"your code goes here\" Implement the void substrcpy(char s

ID: 653542 • Letter: A

Question

Answer where it says "your code goes here"

Implement the void substrcpy(char str1[], int start, int end, char str2[]) function. it has four parameters:

str1: the source string, a part of it will be copied into str2.

start: a starting position

end: an ending position

str2: the "destination" string

#include
#include

/* Define a Passenger struct */
typedef struct {
    char class[10];
    char age[10];
    char gender[10];
    char survived[10];
} Passenger;

/* Print the information of a Passenger */
void printInfo(Passenger p) {
     

  

   
}

/* Find character ch in string str and return its index */
int findchar(char str[], int size, int start, char ch) {

   
}

/* Copy a substring of str1 into str2 */
void substrcpy(char str1[], int start, int end, char str2[]) {
    /* your code goes here */
   
}

int main() {

    char str[] = "class=first,age=adult,gender=male,survived=yes";
    int len = strlen(str);
    Passenger p;
    int i1, i2;
   
    // Find the first '=' and ',' in str,
    // and copy the substring between them into p.class
    i1 = findchar(str, len, 0, '=');
    i2 = findchar(str, len, i1, ',');
    substrcpy(str, i1 + 1, i2 - 1, p.class);
   
    // Find the second '=' and ',' in str,
    // and copy the substring between them into p.age
    i1 = findchar(str, len, i2, '=');
    i2 = findchar(str, len, i1, ',');
    substrcpy(str, i1 + 1, i2 - 1, p.age);

    // Find the third '=' and ',' in str,
    // and copy the substring between them into p.gender
    i1 = findchar(str, len, i2, '=');
    i2 = findchar(str, len, i1, ',');
    substrcpy(str, i1 + 1, i2 - 1, p.gender);

    // Find the last '=' in str,
    // and copy the substring between it and the end into p.survived
    i1 = findchar(str, len, i2, '=');
    substrcpy(str, i1 + 1, len - 1, p.survived);

    // Print the information of Passenger p
    printInfo(p);
}

Explanation / Answer

void printInfo(Passenger p)
{
   printf("class:%s Age:%s Gender:%s Survived:%s",p.class,p.age,p.gender,p.survived);
}

int findchar(char str[], int size, int start, char ch)
{
for(int i=start;i<size;i++)
{
if(str[i]==ch){return i;}
}
return -1;   
}

void substrcpy(char str1[], int start, int end, char str2[])
{
int j=0;
for(int i=0;i<strlen(str1);i++)
printf("%d",str1[i]);
for(int i=start;i<=end;i++){*(str2+j)=str1[i];j++;}
j--;
*(str2+j)='/0';   
}

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