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

Implement the int findchar(char str[], int size, int start, char ch) function. I

ID: 653539 • Letter: I

Question

Implement the int findchar(char str[], int size, int start, char ch) function.

It has four input parameters:

str: an input string, size: the length of str,    start: a starting position ie an index between 0 and (size -1), ch: a single character

Given the inputs this function searches the character ch in the substring of str which starts from the index position specified by start and goes all the way to the end of str. if start is 0, then the entire string will be searched. NOte that, the character being searched may appear multiple times in the (sub) string, and the function returns the index of the first appearance of the searched character. if the character is not found, the function should return -1.

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

/* 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) {
    **********/* your code goes here */******
   
}

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

   
}

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';   
}