C++ strcat()用法及代码示例 | 您所在的位置:网站首页 › strcar › C++ strcat()用法及代码示例 |
在 C/C++ 中,strcat() 是用于字符串处理的预定义函数,位于字符串库(C 中的 string.h 和 C++ 中的 cstring)下。 此函数将 src 指向的字符串附加到 dest 指向的字符串的末尾。它将在目标字符串中附加源字符串的副本。加上一个终止的 Null 字符。字符串(src)的初始字符会覆盖字符串(dest)末尾的 Null-character。 如果出现以下情况,则行为未定义: 目标数组对于 src 和 dest 的内容以及终止的空字符都不够大如果字符串重叠。如果 dest 或 src 不是指向以 null 结尾的字节字符串的指针。用法: char *strcat(char *dest, const char *src)参数:该方法接受以下参数: dest:这是一个指向目标数组的指针,它应该包含一个 C 字符串,并且应该足够大以包含连接的结果字符串。src:这是要附加的字符串。这不应与目的地重叠。返回值:strcat()函数返回目标字符串的指针dest。 应用:给定C++中的两个字符串src和dest,我们需要将src指向的字符串追加到dest指向的字符串的末尾。 例子: Input:src = "ForGeeks" dest = "Geeks" Output:"GeeksForGeeks" Input:src = "World" dest = "Hello " Output:"Hello World"下面是实现上述方法的 C 程序: C // C program to implement // the above approach #include #include // Driver code int main(int argc, const char* argv[]) { // Define a temporary variable char example[100]; // Copy the first string into // the variable strcpy(example, "Geeks"); // Concatenate this string // to the end of the first one strcat(example, "ForGeeks"); // Display the concatenated strings printf("%s\n", example); return 0; }输出: GeeksForGeeks注意: 目标字符串应该足够大以容纳最终字符串。 |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |