/*Answer where it says \"your code goes here\" Implement the void substrcpy(char
ID: 653696 • Letter: #
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
/* Copy a substring of str1 into str2 */
void substrcpy(char str1[], int start, int end, char str2[]) {
str2.assign( str1, start, end );
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.