Write a function copystr(char *p1, char *p2, int m) that takes two strings and o
ID: 3808480 • Letter: W
Question
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>
int main(void){
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();
}
return 0;
}
void copystr(char *p1, char *p2, int m){
/* write your code here*/
int len = strlen(p1);
int ind = 0;
for(int i=m-1;i<len;i++){
p2[ind++] = p1[i];
}
}
OUTPUT :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.