strcat

strcat

將兩個char類型鏈接。char d="GoldenGlobal"; char *s="View"; strcat(d,s);結果放在d中printf("%s",d);輸出 d 為 GoldenGlobalView (中間無空格)d和s所指內存區域不可以重疊且d必須有足夠的空間來容納s的字元串。返回指向d的指針。

C函數


C函數原型
strcat
strcat
extern char *strcat(char *dest,char *src);
用法
#include
在C++中,則存在於頭文件中。
功能
把src所指字元串添加到dest結尾處(復蓋dest結尾處的'\0')並添加'\0'。
說明
strcat
strcat
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;
// 後文return address,故不能放在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.