连接两个字符串:连接两个字符串的几种方法

原帖及讨论:http://bbs.bc-cn.net/dispbbs.asp?boardid=5&id=85644

题目:编一程序,将两个字符串连接起来,不要用strcat函数。
第1种方法:

main()
{
char str1[10]=\"world!\";
char str2[20]=\"hello \";
strcpy(str2+strlen(str2),str1);
printf(\"%s\",str2);
}

第2种方法:

void copy_string(char from[],char to[])
{
int i=6;
while(from[i]!=\'\\0\')
{to[i]=from[i];i++;}
to[i]=\'\\0\';
}
main()
{
char a[]=\"word!\";
char b[11]=\"hello \";
copy_string(a,b);
printf(\"\\n%s\\n\",b);
}

由于我是菜鸟,第一种方法是在别人的帮忙下完成的,而第二种方法是自己想出来的。
不知道符不符合题目要求呢?
你们有别的见解的话,发表发表上来哦~

论坛会员论坛提供的方法:

程序代码:
#include<stdlib.h>
#include<stdio.h>


#definebuffsize80


intmain(void)
{
chars1[buffsize]=\"hello\",s2[]=\"china\";


sprintf(s1,\"%s%s\",s1,s2);
printf(\"s1=%s\\n\",s1);


exit(0);
}

论坛会员nicksharp提供的方法:

程序代码:
#include<stdio.h>
voidcontact(charstr[],constcharstr1[],constcharstr2[])
{
ini,j;
for(i=0;str1[i]!=’\\0’;i++)
str[i]=str1[i];

for(j=0;str2[j]!=’\\0’;j++)
str[i+j]=str2[j];

str[i+j]=’\\0’
}
intmain(void)
{
voidcontact(charstr[],constcharstr1[],constcharstr2[]);

constchars1[]={\"test\"};
constchars2[]={\"works.\"};
chars3[12];
contact(s3,s1,s2);
printf(\"%s\",s3);

getch();
return0;
}
Tags:  oracle连接字符串 sql连接字符串 连接字符串 连接两个字符串

延伸阅读

最新评论

发表评论