strcat

strcat

连接字符串的函数
strcat是连接字符串的函数。函数返回指针,两个参数都是指针,第一个参数所指向的内存的地址必须能容纳两个字符串连接后的大小。
    中文名:strcat 外文名:strcat() 用法:#include 类型:函数 性质:c语言

C函数

C函数原型

extern char *strcat(char *dest,char *src);

用法

#include

在C++中,则存在于头文件中。

功能

把src所指字符串添加到dest结尾处(复盖dest结尾处的'0')并添加'0'。

说明

src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。

返回指向dest的指针

举例

//strcat.c

#include

#include

main()

{

char d="Golden Global";

char *s="View";

clrscr();

strcat(d,s);

printf("%s",d);

getchar();

return 0;

}

程序执行结果为:

Golden Global View

Strcat函数原型如下:

char *strcat(char *strDest, const char *strSrc)//将源字符串加const,表明其为输入参数

{

char *address=strDest;

// 后文returnaddress,故不能放在assert断言之后声明address

assert((strDest!=NULL)&&(strSrc!=NULL));//对源地址和目的地址加非0断言

while(*strDest)

//是while(*strDest!=’0’)的简化形式

{

//若使用while(*strDest++),则会出错,因为循环结束后strDest还会执行一次++,那么strDest

strDest++;//将指向'0'的下一个位置。/所以要在循环体内++;因为要是*strDest最后指

}

//向该字符串的结束标志’0’。

while(*strDest++=*strSrc++)

{

NULL;

//该循环条件内可以用++,

}

//此处可以加语句*strDest=’0’;无必要

return address;

//为了实现链式操作,将目的地址返回

}

MATLAB函数

定义

strcat即Strings Catenate,横向连接字符串。

语法

combinedStr=strcat(s1,s2,...,sN)

描述

将数组s1,s2,...,sN水平地连接成单个字符串,并保存于变量combinedStr中。如果任一参数是元胞数组,那么结果combinedStr是一个元胞数组,否则,combinedStr是一个字符数组。

实例

>>a='Hello'

a=

Hello

>>b=' Matlab'

b=

Matlab

>>c=strcat(a,b)

c=Hello Matlab

附注

For character array inputs,strcat removes trailing ASCII white-spacecharacters:space,tab,vertical tab,newline,carriage return,and form-feed.To preserve trailing spaces when concatenating character arrays,use horizontal array concatenation,[s1,s2,...,sN].See the final example in the following section.

For cell array inputs,strcat does not remove trailing white space.

When combining nonscalar cell arrays and multi-row character arrays,cell arrays must be column vectors with the same number of rows as the character arrays.

用法

头文件:#includenstrcat()函数用来连接字符串,其原型为:nchar*strcat(char*dest,const char*src);n【参数】dest为目的字符串指针,src为源字符串指针。nstrcat()会将参数src字符串复制到参数dest所指的字符串尾部;dest最后的结束字符NULL会被覆盖掉,并在连接后的字符串的尾部再增加一个NULL。n注意:dest与src所指的内存空间不能重叠,且dest要有足够的空间来容纳要复制的字符串。n【返回值】返回dest字符串起始地址。

相关词条

相关搜索

其它词条