![]() |
#2
rjsp2020-03-13 09:38
|
而我直接这样写"cout<<str1[0]<<str1[1]<<……;",是可以输出的。
我也查了一下在C++中string类型的变量直接写名称是可以输出的,不知道为什么在这里就不行了
题目:有一字符串,包含n个字符。写一函数将此字符串中从第m个字符开始的全部字符复制成为另外一个字符串。
代码:

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string str;
void Partial_replication(string);
cout<<"请输入一个字符串:"<<endl;
cin>>str;
Partial_replication(str);
return 0;
}
void Partial_replication(string str)
{
int StringLength(string str);
string str1;
int n,m,i=0;
char *p,*p1;
n=StringLength(str);
cout<<"请输入被复制的字符个数m,(m<"<<n<<"):" ;
cin>>m;
for(p=&str[m-1],p1=&str1[0];p<=&str[n-1];p++,p1++)
*p1=*p;
cout<<"被复制的部分字符串为:"<<str1<<endl;
}
int StringLength(string str)
{
char *p;
int i=0;
for(p=&str[0];*p!='\0';p++)
i++;
return i;
}
#include<string>
using namespace std;
int main(void)
{
string str;
void Partial_replication(string);
cout<<"请输入一个字符串:"<<endl;
cin>>str;
Partial_replication(str);
return 0;
}
void Partial_replication(string str)
{
int StringLength(string str);
string str1;
int n,m,i=0;
char *p,*p1;
n=StringLength(str);
cout<<"请输入被复制的字符个数m,(m<"<<n<<"):" ;
cin>>m;
for(p=&str[m-1],p1=&str1[0];p<=&str[n-1];p++,p1++)
*p1=*p;
cout<<"被复制的部分字符串为:"<<str1<<endl;
}
int StringLength(string str)
{
char *p;
int i=0;
for(p=&str[0];*p!='\0';p++)
i++;
return i;
}
这是我的运行结果:
只有本站会员才能查看附件,请 登录