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.
#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 |
|---|---|
| Returns the length of the string. | |
| Copy one string to another. | |
strncpy() | Copy first n characters of one string to another. |
| Concatenates two strings. | |
| Concatenates first n characters of one string to another. | |
| Compares two strings. | |
strncmp() | Compares first n characters of two strings. |
| Find the given substring in the string. | |
| Returns the span of the source string not containing any character of the given string. | |
| Returns the span of the source string containing only the characters of the given string. | |
| Finds the first occurrence of any of the characters of the given string in the source string. | |
| Initialize a block of memory with the given character. | |
| Copy two blocks of memory. | |
| Moves two blocks of memory. |
Example
#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 with | C-style character arrays (char[]) | std::string objects |
| Memory management | Manual | Automatic |
| Resizing | Fixed-size arrays | Dynamic resizing |
| Safety | More error-prone | Safer and easier to use |
| Performance | Generally faster with lower overhead | Slightly slower due to automatic management |
| Availability | C 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++ header | C header |
| Declares functions in the std namespace (implementation-dependent global namespace availability) | Declares functions in the global namespace |
| Recommended for modern C++ programs | Used 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.