strncat() function in C/C++
Last Updated :
12 Oct, 2021
In C/C++, strncat() is a predefined function used for string handling. string.h is the header file required for string functions.
This function appends not more than n characters from the string pointed to by src to the end of the string pointed to by dest plus a terminating Null-character. The initial character of the string(src) overwrites the Null-character present at the end of a string(dest). Thus, the length of the string(dest) becomes strlen(dest)+n. But, if the length of the string(src) is less than n, only the content up to the terminating null-character is copied and the length of the string(dest) becomes strlen(src) + strlen(dest).
The behavior is undefined if -
- The strings overlap.
- The dest array is not large enough to append the contents of src.
Syntax:
char *strncat(char *dest, const char *src, size_t n)
Parameters: This method accepts the following parameters:
- dest: the string where we want to append.
- src: the string from which 'n' characters are going to append.
- n: represents a maximum number of characters to be appended. size_t is an unsigned integral type.
Return Value: The strncat() function shall return the pointer to the string(dest).
Application
Given two strings src and dest in C++, we need to append 'n' character from src to dest, let's say n=5.
Examples:
Input: src = "world"
dest = "Hello "
Output: "Hello world"
Input: src = "efghijkl"
dest = "abcd"
Output: "abcdefghi"
Program:
C++
// C,C++ program demonstrate functionality of strncat()
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
// Take any two strings
char src[50] = "efghijkl";
char dest[50]= "abcd";
// Appends 5 character from src to dest
strncat(dest, src, 5);
// Prints the string
cout <<"Source string : "<< src << endl;
cout <<"Destination string : "<< dest;
return 0;
}
// This code is contributed by shivanisinghss2110
C
// C,C++ program demonstrate functionality of strncat()
#include <stdio.h>
#include <string.h>
int main()
{
// Take any two strings
char src[50] = "efghijkl";
char dest[50]= "abcd";
// Appends 5 character from src to dest
strncat(dest, src, 5);
// Prints the string
printf("Source string : %s\n", src);
printf("Destination string : %s", dest);
return 0;
}
Output:
Source string : efghijkl
Destination string : abcdefghi
How strncat() is different from strcat() ?
It is recommended by many of the programmers that strncat() is safe as compared to strcat() because strcat() does not check for the size of the copied data, and copies until it gets to a null terminator, it might cause a buffer overflow while strncat() check for the size of the copied data, and will copy only 'n' bytes.
C++
// C,C++ program demonstrate difference between
// strncat() and strcat()
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
// Take any two strings
char src[50] = "forgeeks";
char dest1[50] = "geeks";
char dest2[50] = "geeks";
cout << "Before strcat() function execution, ";
cout << "destination string : "<< dest1 << endl;
// Appends the entire string of src to dest1
strcat(dest1, src);
// Prints the string
cout << "After strcat() function execution, ";
cout << "destination string : "<< dest1 << endl;
cout << "Before strncat() function execution, ";
cout << "destination string : "<< dest2 << endl;
// Appends 3 characters from src to dest2
strncat(dest2, src, 3);
// Prints the string
cout << "After strncat() function execution, ";
cout << "destination string : "<< dest2 << endl;
return 0;
}
// this code is contributed by shivanisinghss2110
C
// C,C++ program demonstrate difference between
// strncat() and strcat()
#include <stdio.h>
#include <string.h>
int main()
{
// Take any two strings
char src[50] = "forgeeks";
char dest1[50] = "geeks";
char dest2[50] = "geeks";
printf("Before strcat() function execution, ");
printf("destination string : %s\n", dest1);
// Appends the entire string of src to dest1
strcat(dest1, src);
// Prints the string
printf("After strcat() function execution, ");
printf("destination string : %s\n", dest1);
printf("Before strncat() function execution, ");
printf("destination string : %s\n", dest2);
// Appends 3 characters from src to dest2
strncat(dest2, src, 3);
// Prints the string
printf("After strncat() function execution, ");
printf("destination string : %s\n", dest2);
return 0;
}
Output:
Before strcat() function execution, destination string : geeks
After strcat() function execution, destination string : geeksforgeeks
Before strncat() function execution, destination string : geeks
After strncat() function execution, destination string : geeksfor
Similar Reads
strcat() Function in C++
The strcat() function in C++ is a predefined function in the <cstring> header file that is used to concatenate two strings, by appending a copy of the source string to the end of the destination string. This function works by adding all the characters till the null character of the source stri
2 min read
wcsncat() function in C/C++
The wcsncat() function appends the characters of the source to the destination, including a terminating null wide character. If the length of the string in source is less than num. Then only the content up to the terminating null wide character is copied. Syntax: wchar_t* wcsncat (wchar_t* destinati
2 min read
strtod() function in C/C++
The strtod() is a builtin function in C and C++ STL which interprets the contents of the string as a floating point number and return its value as a double. It sets a pointer to point to the first character after the last valid character of the string, only if there is any, otherwise it sets the poi
4 min read
strcspn() function in C/C++
The strcspn() function in C/C++ takes two string as input, string_1 and string_2 as it's argument and searches string_1 by traversing for any characters that is present in string_2 . The function will return the length of string_1 if none of the characters of string_2 are found in string_1 . This fu
2 min read
strnset() function in C
The strnset() function is a builtin function in C and it sets the first n characters of a string to a given character. If n is greater than the length of string, the length of string is used in place of n. Syntax: char *strnset(const char *str, char ch, int n); Parameters: str: This is the original
2 min read
strlen() function in c
The strlen() function in C calculates the length of a given string. The strlen() function is defined in string.h header file. It doesn't count the null character '\0'. Syntax of C strlen() The syntax of strlen() function in C is as follows: size_t strlen(const char* str);Parameters The strlen() func
1 min read
strspn() function in C
The strspn() function returns the length of the initial substring of the string pointed to by str1 that is made up of only those character contained in the string pointed to by str2. Syntax : size_t strspn(const char *str1, const char *str2) str1 : string to be scanned. str2 : string containing the
1 min read
wcsrchr() function in C/C++
The wcsrchr() function is a builtin function in C/C++ which searches for the last occurrence of a wide character in a wide string. It is defined within the cwchar header file in C++. Syntax: wcsrchr(str, ch) Parameters: The function accepts two parameters which are described below. str: It specifies
2 min read
wcsncmp() function in C/C++
The wcsncmp() function in C/C++ compare characters of two wide strings. The comparison is done lexicographically. This function takes three arguments lhs, rhs and count. It compares the contents of lhs and rhs lexicographically upto a maximum of count wide characters. Note: The behaviour of wcsncmp(
3 min read
vswprintf() function in C/C++
This vswprintf() function writes the wide string to the wide string buffer. A maximum of (len-1) wide characters are written to buffer which is followed by a null wide character. Syntax: int vswprintf( wchar_t* ws, size_t len, const wchar_t* format, va_list arg ) Parameters: The function accepts fou
3 min read