c++看看哪错了

来源:百度知道 编辑:UC知道 时间:2024/05/29 07:41:25
#include <iostream>
using namespace std;
void copystr(char *p1,char *p2,int m){
int i=0;
int mm=m;
while((*(p1+i))!='\0'){
if(i>=mm){
*(p2+i)=*(p1+i);
}
i++;
}
}
int main(){
void copystr(char *,char *,int);
int m=3;
char str1[20]={"12345678"},str2[20];

copystr(str1,str2,m);
cout<<"result:"<<str2<<endl;

cout<<"input string:";
gets(str1);
cout<<"which character do you want begin to copy?";
cin>>m;
if (strlen(str1)<m){
cout<<"input error!"<<endl;
}else{
copystr(str1,str2,m);
cout<<"result:"<<str2<<endl;
}

return 0;
}

void copystr(char *p1,char *p2,int m){
int i=0;
int mm=m;
while((*(p1+i))!='\0'){
if(i>=mm){
*(p2+i-mm)=*(p1+i);//目标字符串p2的起始位置是从i-mm开始;
}
i++;
}
*(p2+i-mm)='\0';//设置最后字符串的结束标志
}

copystr要改成这样:

void copystr(char *p1,char *p2,int m){
int i=0;
int j=0;
int mm=m-1;
while((*(p1+i))!='\0'){
if(i>=mm){
*(p2+j)=*(p1+i);
j++;
}
i++;
}
*(p2+j)='\0';
}