-->





strncat function c++ example source code

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: ";
    gets(str1);

    cout<<"\nEnter second string: ";
    gets(str2);

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

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

          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

0 comments:

Post a Comment