×

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

C++ STL (Standard Template Library)

Introduction C++ is a flexible type and general proposed programming language. So we need a standard library that supports C++. C++ STL (Standard Template Library) is a collection of templates that...

6 minutes read.

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

3 minutes read.

Palindrome Using While Loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

Convex hull Algorithm in C++

The intersection of all convex sets containing a certain subset of a Euclidean space, or alternatively, the set of all convex combinations of points in the subset, defines the convex...

4 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

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...

4 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.

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.

Difference between C and C++

What do you mean by C? C is a machine-independent structure or procedural oriented computer language that is widely utilized in a variety of applications. C is a fundamental programming language...

4 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.