Using strcat and strncat to append two string together - C++ Data Type

C++ examples for Data Type:char array

Description

Using strcat and strncat to append two string together

Demo Code

#include <iostream> 
#include <cstring> // prototypes for strcat and strncat 
using namespace std; 

int main() /*from  w  w w . j a  v  a 2s .c  om*/
{ 
    char s1[ 20 ] = "Happy test test"; 
    char s2[] = "New Year "; // length 9 
    char s3[ 40 ] = ""; 

    cout << "s1 = " << s1 << "\ns2 = " << s2; 

    strcat( s1, s2 ); // concatenate s2 to s1 (length 15) 

    cout << "\n\nAfter strcat(s1, s2):\ns1 = " << s1 << "\ns2 = " << s2; 

    // concatenate first 6 characters of s1 to s3 
    strncat( s3, s1, 6 ); // places '\0' after last character 

    cout << "\n\nAfter strncat(s3, s1, 6):\ns1 = " << s1 
       << "\ns3 = " << s3; 

    strcat( s3, s1 ); // concatenate s1 to s3 
    cout << "\n\nAfter strcat(s3, s1):\ns1 = " << s1 << "\ns3 = " << s3 << endl; 
}

Result


Related Tutorials