字符串连接

头文件<stdio.h>里包含了字符串连接函数strcat()和strncat()的声明。两个函数的原型方别是:

 char *strcat(char *destin, char *source); //将字符串source连接到字符串destin后边。

#include <stdio.h>
#include<conio.h>

int main(void)
{
 char s1[] = "1234";

 char s2[] = "abcdefg" ;

 strcat(s1,s2);

 printf("%s\n", s1);

 getch();

 return 0;
}

输出结果:1234abcdefg

char *strncat(char *destin,char *source ,int n) //将字符串source前n个字符连接到字符串destin后边。

 #include <stdio.h>
#include<conio.h>

int main(void)
{
 int n = 3;

 char s1[] = "1234";

 char s2[] = "abcdefg" ;

 strncat(s1, s2, n);

 printf("%s\n", s1);

 getch();

 return 0;
}

输出结果:1234abc;


在使用strcat()函数与strncat()函数时可以不考虑是否会溢出,strcat()函数与strncat()函数会重新分配存储空间。

#include <stdio.h>
#include<conio.h>

int main(void)
{
 int n = 3;

 char s1[5] = "1234";

 char s2[] = "abcdefg" ;

 strncat(s1, s2, n);

 printf("%s\n", s1);

 getch();

 return 0;
}

输出结果依旧是:1234abc


#include <stdio.h>
#include<conio.h>

int main(void)
{
 int n = 3;

 char s1[5] = "1234";

 char s2[] = "abcdefg" ;

 strcat(s1, s2);

 printf("%s\n", s1);

 getch();

 return 0;
}


输出结果依旧是:1234abcdefg







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值