C plus plus assignment help service

strncat function c++ example source code

Leave a Comment
strncat function used to concatenate two strings upto specified number of characters. It takes 3 parameters first and second is string third is n where n is number of characters to append first string from second string.
For example
first_string: Hello
second_string: World

strncat( first_string, second_string, 3);

The result will be: Hello Wor

Example program
Write a c++ program which takes 3 inputs from two strings and third is the value of n( the number of characters to append from second string) and display the resultant string.

strncat function C++ source code

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main()
    char str1[100],str2[100];

    cout<<"\nEnter first string: ";

    cout<<"\nEnter second string: ";

    int n;
    cout<<"\nEnter number of characters to concatenat: ";
    strncat(str1, str2, n);

    cout<<"\n\n\t\Resultant first String after concatenation: "<<endl;

          return 0;

Program output
strncat string class function source code example
It is recommended to change the code and examine the output of program.
See Also: strcat function example c++ source code


Post a Comment