}R7-4 使用函数实现字符串复制
分数 10
入门
全屏浏览
切换布局
作者 王秀
单位 福州大学
输入一个正整数 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 <iostream>
using namespace std;
#include <string>
void strmcpy(string &s,const string &t,int m)
{
if(m>=0&&m<=t.length())
{
s=t.substr(m,t.length()-m);
}
else
{
s="";
}
}
int main()
{
int repeat,m;
cin>>repeat;
cin.get();
string t,s="";
while(repeat--)
{
getline(cin,t);
cin>>m;
strmcpy(s,t,m);
cout<<s<<endl;
}
return 0;
}
我写的代码运行结果
(repeat=3)
(repeat=3)
(repeat=3)
没结果也不知道为什么