输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:
输入一个字符串 t 和一个正整数 m,将字符串 t 中从第 m 个字符开始的全部字符复制到字符串 s 中,再输出字符串 s。
要求定义并调用函数 strmcpy(s,t,m), 它的功能是将字符串 t 中从第 m 个字符开始的全部字符复制到字符串 s 中,函数形参s和t的类型是字符指针,形参m的类型是int,函数类型是void。
输入输出示例:括号内为说明,无需输入输出
输入样例:
3 (repeat=3)
happy new year
7
happy
1
new
4
输出样例:
new year (从"happy new year"第7个字符开始组成的新字符串为"new year")
happy (从"happy"第1个字符开始组成的新字符串为"happy")
error input ("new"的长度小于4)
#include <string.h>
#include<iostream>
using namespace std;
void strmcpy(char *s,char *t,int m)
{
int i,j=0;
if(strlen(t)>=m)
{
for(i=m-1;t[i]!='\0';i++)
s[j++]=t[i];
s[j]='\0';
cout<<s<<endl;
}
else
{
cout<<"error input"<<endl;
}
}
int main()
{
int repeat,n;
char t[20],s[20];
cin>>repeat;
for(int i=0;i<repeat;i++)
{
getchar();
cin.getline(t,20);
cin>>n;
strmcpy(s,t,n);
}
}
注意:
在输入样例进行测试的时候,一定要记得把3后面的repeat和空白符删除,否则输出为空!