×

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them.

strcat()

C++ is a computer language with a wide range of capabilities. strcat is an intrinsic function of the string class that, like other functions like as strncat and strcpy, has its own significance. These functions are critical because they are associated with code optimization and function calls. strcat() is a two-part function. str refers to the string, while cat refers to appending a copy of the string to the end or beginning of the string. strcat() appends a copy of the source string to the end of the destination string. In this programme, we will use two parameters:

  1. dest01
  2. src01

It will duplicate the source string and append it to the destination string. Src01's first character substitutes the terminating character at the end of dest01.

The value returned is: The strcat() function returns a reference to the dest01 target string.

C++ Program:

// To show strcat, use the CPP code.
#include <cstring>
#include <iostream>
using namespace std;
int main ()
{
	char dest01 [50] = "Hello Mr.";
	char src01 [50] = " saha...";


	strcat( dest01, src01 );
	cout << dest01 ;
	return 0;
}

Output:

Hello Mr. saha...

strncat()

The strncat() function in C++ appends the supplied number of characters to the end of one string.

We will utilise three parameters in this program:

  1. Dest01
  2. Src01
  3. Count01

This adds the provided number of characters to the end of the dest01 string from the src01 string. The first character of the src01 string replaces the last character of the dest01 string.

strncat() returns a reference to the target string, dest01.

Return: The dest parameter of the strcat function returns the destination string. If the destination string is null, the strcat method will result in overlapping, which implies that all characters will be replaced with a copy of the source string. One caveat: the destination should never, ever overlap; if dest is an empty string, the possibility exists; otherwise, the destination should never, ever overlap. Furthermore, the Pointer to the target location must be large enough to hold or contain the concatenated content.

C++ Program:

// To show strncat (), use the CPP code.
#include <cstring> 
#include <iostream>


using namespace std;
int main()
{
	char dest01 [25] = "For instance,we";
	char src01 [50] = " will show working of strncat() extra words";
	
strncat ( dest01, src01, 29 );
	cout << dest01 ;
	return 0;
}

Output:

For instance,we will show working of strncat

What makes strncat() different from strcat()?

Many programmers believe strncat() is safer than strcat() because strcat() does not check for the amount of duplicated data and copies until it encounters a null terminator, which may cause a buffer overflow, whereas strncat() checks for the amount of copied data and copies just 'n' bytes.

C Program:

// C programme to show the distinction between strncat() and strcat()
#include <stdio.h>
#include <string.h>
// Main function
int main()
{
// considering two strings
char src01 [50] = "Hi man";
char dest01 [50] = "Hello";
char dest02 [50] = "Hello";
// printing of the string
printf( "Before strcat() function execution, " );
printf( "destination string : %s\n", dest01 );
	
// Generates the whole src01 string to dest01.
strcat( dest01, src01 ) ;


// Printing of the string
printf( "After strcat() function execution, " );
printf( "destination string : %s\n", dest01 );
// Printing the string
printf( "Before strncat() function execution, " );
printf( "destination string : %s\n", dest02 );
	
// Generates 3 characters from src01 to dest02 in this way.
strncat( dest02, src01, 3 );
	
// Printing the string
printf( "After strncat() function execution, " );
printf( "destination string : %s\n", dest02 );
	
return 0;
}

C++ Program:

// C++ programme to show the distinction between strncat() and strcat() ()
#include<bits/stdc++.h>
#include <string.h>
using namespace std;
// Main function
int main()
{


// considering two strings
char src01 [50] = "Hi man";
char dest01 [50] = "Hello";
char dest02 [50] = "Hello";
// Printing the string
cout << "Before strcat() function execution, " ;
cout <<  "destination string : " << dest01 ;
	
// Generates the whole src01 string to dest01 in this way.
strcat( dest01, src01 ) ;


// Printing of the string
cout << "\nAfter strcat() function execution, " ;
cout << "destination string : " << dest01 ;
// Printing the string
cout << "\nnBefore strncat() function execution, " ;
cout << "destination string : " << dest02 ;
	
// Generates 3 characters from src01 to dest02
strncat( dest02, src01, 3 );
	
// Printing the string
cout << "\nAfter strncat() function execution, " ;
cout <<  "ndestination string : " << dest02 ;
	
return 0;
}

Output:

Before strcat() function execution, destination string : Hello
After strcat() function execution, destination string : HelloHi man
nBefore strncat() function execution, destination string : Hello
After strncat() function execution, ndestination string : HelloHi

Conclusion

The strcat function in C++ is an extremely useful function that is included in the string.h header file's predefined library. The strncat function is also included in the string.h header file, however it differs from strcat() in terms of concatenation and appending source and destination. It has given the programmers the freedom to call the function at any point throughout the program's flow or outside the scope of the code block. In C++, strncat() is a string manipulation method defined in the cstring header file that operates on a string stored in a c-style char array and is used to append the first n characters of a string to the end of another string.


Related Topics

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Lexicographically Next Permutation in C++

In this tutorial, we'll look at how to use C++ to generate the lexicographically next permutation of a string. The lexicographically next permutation is the larger permutation. "ACB," for example,...

3 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

C++ Interfaces

The C++ programming language provides programmers with a variety of capabilities and functions. It also enables object-oriented programming, which is essential while working on a project. It will be simple...

7 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

C++ Virtual Destructor

In C++, a destructor is a class member function that is used to free up space or remove an object of the class that has gone out of scope. The...

4 minutes read.

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

7 minutes read.

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

How to create a stack in C++

A stack is a data structure, which is of linear type. A specific order has to be followed while inserting and deleting the elements from the stack. Generally, stack follows...

5 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

Counting Frequencies of Array Elements in C++

We have an array of integer items with duplicate values, and our objective is to compute the frequencies of the different elements in the array. Methods: Methods that can be used to...

3 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.