The <cstring> library is a part of the standard C++ library collection that provides the commonly used methods for C-Style string manipulation. It is inherited from the <string.h> library of C language.
We can import the <cstring> header file using #include preprocessor directive and all the <string.h> library function will be available to use in C++.
Syntax:
#include <cstring>
Example:
// C++ program to demonstrate the use of <cstring> header
// File
#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
<cstring> Functions
<cstring> header file contains some useful string manipulation functions. Some of these functions are listed below:
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. |
strchr() | Find the first occurrence of the given character in the string. |
strrchr() | Finds the last occurrence of the given characters in the string. |
| 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. | |
strtok() | Split the given string into tokens based on some character as a delimiter. |
| Initialize a block of memory with the given character. | |
memcmp() | Compares two blocks of memory. |
| Copy two blocks of memory. | |
| Moves two blocks of memory. | |
memchr() | Finds the given character in the block of memory. |
Example:
// C++ program to demonstrate the use of some common <cstring> library functions
#include <iostream>
#include <cstring>
using namespace std;
int main() {
// initializing some strings
char str1[20] = "Geeks";
char str2[20] = "gfg";
char str3[20] = "GeeksforGeeks";
// using strlen()
cout << "Str1 length: " << strlen(str1) << endl;
// using strcat()
cout << "str1 before concatenation: " << str1 << endl;
strcat(str1, str2);
cout << "str1 after concatenation: " << str1 << endl;
// using strcpy()
cout << "str1 before copy: " << str1 << endl;
strcpy(str1, str3);
cout << "str1 after copy: " << str1 << endl;
return 0;
}
Output
Str1 length: 5 str1 before concatenation: Geeks str1 after concatenation: Geeksgfg str1 before copy: Geeksgfg str1 after copy: GeeksforGeeks
Difference between <cstring> and <string>
Following are the differences between cstring and std::string which are defined inside cstring and string header files respectively.
Parameter | <cstring> | <string> |
|---|---|---|
| Definition | <cstring> header file contains some useful functions that work on C-style strings. | <string> header file imports the string class and its member and non-member functions to work on C++ style strings. |
| Implemented Data Type | <cstring> functions work only on the array of characters type. | <string> is a class that is used to create a string object to store characters and all its functions work only on string objects type. |
| Supporting Languages | It is a part of both C and C++. | It's only the part of C++ only. |
| Memory Management | Memory is managed manually and is usually of fixed size. Generally require less memory as compared to C++ Strings. | Memory is managed automatically by the class. Require more memory for storing required data for better management. |
| Access and Operations' Speed | Faster access and operation speed due to simplicity. | Generally slower because of automatic management. |
- Speed: If you need faster access speed and minimum memory usage, it is better to go with cstring as they are the simplest implementation of the string.
- Convenience: If you prefer convenience and ease of access and operations, then it is better to go with the std::string.
Note: Methods of cstring library will not work on std::string data nor the methods of std::string will work on c style strings, but they can be easily converted another type easily using the methods mentioned here.
2. Difference between <cstring> and <string.h>
- C language doesn't support cstring library. It is only part of standard C++ and is inherited from <string.h>. So if we want our program to run on both C and C++, we should use string.h header file.
- When used in C++, <string.h> header file will import all its identifiers in the global namespace and maybe in the std namespace while <cstring> will import all its identifiers in the std namespace and maybe in the global namespace (depending on the compiler vendor).
3. Should I use <cstring> or <string.h> header file?
If we want our program to run both in C and C++ programming languages, then we should include <string.h> because the <cstring> header file is only part of C++, not C language.