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

In C languange. Write a function copystr(char *p1, char *p2, int m) that takes t

ID: 3808466 • Letter: I

Question

In C languange. Write a function copystr(char *p1, char *p2, int m) that takes two strings and one integer as arguments. It will copy from the mth character of p1 to its end to p2. The main function is provided. It will ask the user to input one string and the number of the character that copystr start coping.

Below is the example run

Input string: reading-room

Which character that begin to copy? 9

result: room

main(){

    int m;

    char str1[100], str2[100];

    printf("input string");

    gets(str1);

    printf("Which chracter that begin to copy?");

    scanf("%d", &m); /*read and integer input and store in m*/

    if (strlen(str1) < m)

        printf("input error!");

    else{

        copystr(str1, str2, m);

        printf("result: %s", str2);

        getchar();

    }

}

copystr(char *p1, char *p2, int m){

    /* write your code here*/

}

Explanation / Answer

#include <stdio.h>
#include <string.h>
void copystr(char *p1, char *p2, int m);
int main()
{
int m;
char str1[100], str2[100];
printf("input string");
gets(str1);
printf("Which chracter that begin to copy?");
scanf("%d", &m); /*read and integer input and store in m*/
if (strlen(str1) < m)
printf("input error!");
else{
copystr(str1, str2, m);
printf("result: %s ", str2);
getchar();
}
}

void copystr(char *p1, char *p2, int m){
/* write your code here*/
int i,j;
for(i=m-1,j=0; i< strlen(p1); i++,j++){
p2[j] = p1[i];
}
}

Output:

sh-4.2$ main                                                                                                                                                                                                                                                           

input stringreading-room                                                                                                                                                                                                                                             

Which chracter that begin to copy?9                                                                                                                                                                                                                                    

result: room