C++ <cstring>

Last Updated : 11 Jul, 2026

The <cstring> header provides a collection of functions for working with C-style strings (null-terminated character arrays). It is the C++ version of the C <string.h> header and contains functions for string handling and memory manipulation.

  • Provides functions for manipulating C-style strings and memory blocks.
  • Declared in the <cstring> header and available in the std namespace.
C++
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    // creating a test strings
    char name[20] = "David ";
    char last_name[20] = "Lauren";

    // initial strings
    cout << "Before Concatenation" << endl;
    cout << "\tName string: " << name << endl;
    cout << "\tString length: " << strlen(name) << endl;
  
    // using strcat() for concatenation
    strcat(name, last_name);

    // final strings
    cout << "After Concatenation" << endl;
    cout << "\tName string: " << name << endl;
    cout << "\tString length: " << strlen(name) << endl;

    return 0;
}

Output
Before Concatenation
    Name string: David 
    String length: 6
After Concatenation
    Name string: David Lauren
    String length: 12

Explanation

  • strlen() returns the length of the string.
  • strcat() appends one C-style string to another.
  • The resulting string becomes "David Lauren".

Syntax

#include <cstring>

Common <cstring> Functions

The <cstring> header provides functions for string manipulation, searching, comparison, tokenization, and memory operations.

Function Name

Function Description

strlen()

Returns the length of the string.

strcpy()

Copy one string to another.

strncpy()

Copy first n characters of one string to another.

strcat()

Concatenates two strings.

strncat()

Concatenates first n characters of one string to another.

strcmp()

Compares two strings.

strncmp()

Compares first n characters of two strings.

strstr()

Find the given substring in the string.

strcspn()

Returns the span of the source string not containing any character of the given string.

strspn()

Returns the span of the source string containing only the characters of the given string.

strpbrk()

Finds the first occurrence of any of the characters of the given string in the source string.

memset()

Initialize a block of memory with the given character.

memcpy()

Copy two blocks of memory.

memmove()

Moves two blocks of memory.

Example

C++
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
    char str1[20] = "Geeks";
    char str2[20] = "gfg";
    char str3[20] = "GeeksforGeeks";

    cout << "Length: " << strlen(str1) << endl;

    strcat(str1, str2);
    cout << "After strcat(): " << str1 << endl;

    strcpy(str1, str3);
    cout << "After strcpy(): " << str1 << endl;

    return 0;
}

Output
Length: 5
After strcat(): Geeksgfg
After strcpy(): GeeksforGeeks

Difference between <cstring> and <string>

The <cstring> and <string> headers are designed for different string representations.

Feature<cstring><string>
Works withC-style character arrays (char[])std::string objects
Memory managementManualAutomatic
ResizingFixed-size arraysDynamic resizing
SafetyMore error-proneSafer and easier to use
PerformanceGenerally faster with lower overheadSlightly slower due to automatic management
AvailabilityC and C++C++ only

Difference Between <cstring> and <string.h>

Although both headers provide the same C string functions, they differ in their intended use.

<cstring><string.h>
C++ headerC header
Declares functions in the std namespace (implementation-dependent global namespace availability)Declares functions in the global namespace
Recommended for modern C++ programsUsed for C compatibility

Choosing Between <cstring> and <string.h>

The choice depends on the language and compatibility requirements.

  • Use <cstring> for modern C++ programs.
  • Use <string.h> when writing C programs or code intended to compile in both C and C++.
  • Prefer std::string over C-style strings for most new C++ applications unless compatibility or low-level memory manipulation is required.
Comment