64,644
社区成员
发帖
与我相关
我的任务
分享
//可以直接用string类库
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str="afagasg";
str+="123";
cout<<str<<endl;
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char * mycpy(char *p, int a);
int count(int a);
int main(int argc, char* argv[])
{
int a = 12345678;
char *p = "9999";
char *pp = mycpy(p, a);
puts(pp);
free(pp);
return 0;
}
char* mycpy(char *p, int a) {
if (!p)
{
printf("Oh, no!\n");
return NULL;
}
int count_of_a = count(a);
count_of_a += strlen(p) + 1;
char *pp = (char *)malloc(count_of_a);
strcpy(pp, p);
sprintf(pp+strlen(p), "%d", a);
return pp;
}
int count(int a) {
if (a == 0)
{
return 1;
}
else {
a /= 10;
if (a == 0)
{
return 1;
}
else
return count(a)+1;
}
}