ฟังก์ชัน strncat() ใน C++ ใช้สำหรับการต่อข้อมูล โดยผนวกจำนวนอักขระที่ระบุจากสตริงต้นทางที่ส่วนท้ายของสตริงปลายทางและส่งคืนตัวชี้ไปยังสตริงปลายทาง ไวยากรณ์ของ strncat() มีดังต่อไปนี้
char * strncat ( char * dest, const char * src, size_t num );
ในไวยากรณ์ข้างต้น src สตริงต้นทางจะถูกต่อท้ายสตริงปลายทางจนถึงอักขระ num เท่านั้น
โปรแกรมที่แสดง strcat() มีดังต่อไปนี้
ตัวอย่าง
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[20] = "Programming is ";
char str2[20] = "fun";
strncat(str1, str2, 3);
cout << "The concatenated string is "<<str1;
return 0;
} ผลลัพธ์
The concatenated string is Programming is fun
ในโปรแกรมข้างต้น สตริงทั้งสองมีการกำหนด str1 และ str2 strncat() ต่อท้ายเนื้อหาของ str2 ที่ส่วนท้ายของ str1 จนถึงอักขระสามตัว และสตริงที่ต่อกันจะแสดงโดยใช้ cout ได้ดังนี้
char str1[20] = "Programming is "; char str2[20] = "fun"; strncat(str1, str2, 3); cout << "The concatenated string is "<<str1;