什么是C语言中的strcat()函数?
C库函数的char*的strcat(字符*DEST,常量字符*SRC)追加由指向的字符串SRC到字符串的末尾通过指向DEST。
字符数组称为字符串。
宣言
以下是数组的声明-
char stringname [size];
例如-charstring[50];长度为50个字符的字符串
初始化
使用单字符常量-
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
使用字符串常量-
char string[10] = “Hello”:;
访问-控制字符串“%s”用于访问字符串,直到遇到“\0”为止。
该strcat()功能
这用于组合或串联两个字符串。
目标字符串的长度必须大于源字符串。
结果级联字符串是源字符串。
语法如下-
strcat (Destination String, Source string);
示例
以下程序显示了strcat()功能的用法。
#include <string.h> main(){ char a[50] = "Hello \n"; char b[20] = "Good Morning \n"; strcat (a,b); printf("concatenated string = %s", a); }输出结果
执行以上程序后,将产生以下结果-
Concatenated string = Hello Good Morning
示例
让我们来看另一个例子。
以下是使用strcat库函数将源字符串连接到目标字符串的C程序-
#include<stdio.h> #include<string.h> void main(){ //Declaring source and destination strings// char source[45],destination[50]; //Reading source string and destination string from user// printf("Enter the source string : \n"); gets(source); printf("Enter the destination string : \n"); gets(destination); //Concatenate all the above results// strcat(source,destination); //Printing destination string// printf("修改后的目标字符串:"); puts(source); }输出结果
执行以上程序后,将产生以下结果-
Enter the source string :nhooo.com Enter the destination string :C programming 修改后的目标字符串:nhooo.com C programming