64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <iterator>
using namespace std;
int main(){
string s1="DN",s2="CS";
copy(s1.begin(),s1.end(),back_inserter(s2));
cout << s2 << endl;
return 0;
}
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
void stradd(char *head,const char *tail,int size)
{
assert(head!=NULL||tail!=NULL);
while((size--)!=0)*head++;
while((*head++=*tail++)!='\0');
}
int main()
{
char a[20]="hello";
char b[10]=" world!";
stradd(a,b,strlen(a));
cout<<a;
return 0;
}
#include "string.h"
char * stradd(char **dest,const char *src)
{
char *temp = NULL;
int len_dest = 0;
int len_src = 0;
int index = 0;
int i = 0;
if(dest == NULL)return NULL;
if(src == NULL || *src == '\0')
{
return *dest;
}
len_src = strlen(src);
if(*dest == NULL)
{
*dest = new char[len_src + 1];
strcpy(*dest,src);
}
else
{
len_dest = strlen(*dest);
temp = new char[len_src + len_dest +1];
while((*dest)[i] != '\0')
{
temp[index] = (*dest)[i];
i++;
index++;
}
i = 0;
while(src[i] != '\0')
{
temp[index] = src[i];
i++;
index++;
}
temp[index] = '\0';
delete [](*dest);
*dest = temp;
}
return *dest;
}
int main(void)
{
char *a = new char[10];
a[0]='a';
a[1]='\0';
char *b = "bcd";
stradd(&a,b);
printf(a);
}