有一字符串,包含n个字符。写一函数copystr(),将此字符串中从第m个字符开始的全部字符复制成为另外一个字符串。部分程序如下:
#include <iostream>
using namespace std;
int main()
{void copystr(char *,char *,int);
int m;
char str1[20],str2[20];
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) //字符串部分复制函数*/
#include <iostream>
#include "string.h"
using namespace std;
void copystr(char *,char *,int);
void main()
{
int m;
char str1[20],str2[20];
cout << "Input string:";
cin >> str1;
cout << "which character do you want begin to copy?"<< endl;
cin >> m;
if(strlen(str1) < m)
cout<<"result:"<<str2<<endl;
else
copystr(str1,str2,m);
}
void copystr(char *p1, char *p2,int m)
{
int i;
for(i = 0; p1[m] != '\0'; i ++,m ++)
p2[i] = p1[m];
p2[i+1] = '\0';
cout << p2;
}
======================
写完整了,把你主体的稍微做了一点点改变。
void copystr(char* p1, char* p2, int m)
{
memset(p2, 0, strlen(p1));
memcpy(p2, p1 + m - 1, strlen(p1) - m + 1);
}