- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
strncat() in C++
The function strncat() in C++ is used for concatenation. It appends the specified number of characters from the source string at the end of the destination string and returns a pointer to the destination string. The syntax of strncat() is given as follows.
char * strncat ( char * dest, const char * src, size_t num );
In the above syntax, the source string src is appended at the end of the destination string dest till num characters only.
A program that demonstrates strcat() is given as follows.
Example
#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; }
Output
The concatenated string is Programming is fun
In the above program, the two strings str1 and str2 are defined. strncat() appends the contents of str2 at the end of str1 till three characters and the concatenated string is displayed using cout. This is given as follows.
char str1[20] = "Programming is "; char str2[20] = "fun"; strncat(str1, str2, 3); cout << "The concatenated string is "<<str1;
Advertisements