Advertisement



< Prev
Next >



C++ strncat() function




In C++, the strncat() is a string manipulation function defined in the cstring header file, which works on a string which is stored in a c-style char array, which is used to add the first n number of characters of a string to the end of another string.




Signature of strncat() function


char * strncat ( char * destination, const char * source, size_t num )







Example of strncat() function


In the upcoming code, we are calling strncat() function, to add first 2 characters of one string named arr2 at the end of characters present in another string named arr1.

//  Program to append n number of characters of first string at the end of another string.

#include<iostream>
#include<cstring>

using namespace std;

int main()
{
char arr1[10] = "Have a ";
char arr2[10] = "good day!";

cout<< "The content of first string is : " << arr1 << "\n";
cout<< "The content of second string is : " << arr2 << "\n";

strncat(arr1,arr2,2); // only first two characters from arr2 will be appended to the end of arr1 

cout<< "The addition of two string is : " << arr1 << "\n";
cout<< "The content of second string : " << arr2;

return 0;
}


Output is -


The content of first string is : Have a
The content of second string is : good day!
The addition of two string in the first string : Have a go
The content of second string : good day!




Advertisement




  • Adding two strings without using strncat()


  • Let's create a program appends the first n number of characters of one string at the end of another string without using strncat() function.

    // Program to append n, number of characters of one string at the end of another string without strcat() function 
    
    
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    
    int main()
    {
    char dest[20] = "Hello";
    char src[10] = "World";
    
    cout<< "The value in dest string : " << dest << "\n";
    
    cout<< "The second in src string : " << src << "\n";
    
    int len1 = strlen(dest);
    int len2 = strlen(src);
    
    cout<< "Length of characters in dest string : " << len1 << "\n";
    
    int num; 
    cout<< "How many characters of the first string you to append : ";
    cin>>num;
    
    for(int i=0;i<num; i++)
    {
    	dest[len1] = src[i];
    	len1 = len1 + 1 ;
    }
    dest[len1]='\0';
    
    cout<< "Concatenated strings : " << dest;
    
    return 0;
    }


    Output is :


    The value in dest string : Hello
    The second in src string : World
    Length of characters in dest string : 5
    Length of characters in src string : 5
    How many characters of the first string you to append : 5
    Concatenated strings : HelloWorld





    Note: - The function strlen returns the total number of characters actually present in the char[] and not the total number of character it can hold.





    Please share this article -





    < Prev
    Next >
    < C++ strcat() function
    C++ strcpy() function >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON


    Advertisement