
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <cstring>
The <cstring> header in C++ provides a set of functions for manipulating c-style strings (null terminated character arrays), and performing operations on memory blocks. The c-strings are the array of characters that end with null character (\0), indicating the end of the string. The <cstring> library is inherited from the c programming language and is a part of the C++ standard library.
The c-style strings are differ from the C++ string objects (std::string) as they are low-level and depends on raw memory manipulations. Working with the C-strings can be more efficient than using the higher-level C++ string classes in case of performance, as they allow direct control over the memory.
The <cstring> library provides a various functions for working such as copying, concatenation, comparison and searching. These functions treats the cstrings as an array of characters and perform operations until they reach the null terminator.
Including <cstring> Header
To include the <cstring> header in your C++ program, you can use the following syntax.
#include <cstring>
Functions of <cstring> Header
Below is list of all functions from <cstring> header.
Copying
These functions are used for manipulating and copying the c-style strings or memory blocks. Each has their own use, depending on whether working with the null-terminated strings or raw memory blocks.
The commonly used C++ <cstring> copying functions are listed below along with their description.
It is used to copy one buffer to another.
It is used to move one buffer to another.
It is used to copy one string to another.
It is used to copy a certain amount of characters from one string to another.
Sr.No | Functions & Description |
---|---|
1 | memcpy |
2 | memmove |
3 | strcpy |
4 | strncpy |
Copying one string to another
In the following example, we are going to copy one string to another.
#include <iostream> #include <cstring> int main() { char x[] = "Welcome"; char y[50]; strcpy(y, x); std::cout << "Result : " << y << std::endl; return 0; }
Output
Output of the above code is as follows −
Result : Welcome
Concatenation
It is the process of joining two or more strings together to form a single string. The commonly used C++ <cstring> concatenation functions are listed below along with their description.
It is used to concatenate two strings.
It is used to concatenate a certain amount of characters of two strings.
Sr.No | Functions & Description |
---|---|
1 | strcat |
2 | strncat |
Concatenating two strings
Consider the following example, we are going to concatenate the two strings.
#include <iostream> #include <cstring> int main() { char a[12] = "123"; char b[] = "456"; strcat(a, b); std::cout << "Result: " << a << std::endl; return 0; }
Output
Following is the output of the above code −
Result: 123456
Comparison
Comparison functions are used to compare strings, determine their order and checking for equality. The commonly used C++ <cstring> comparison functions are listed below along with their description.
It is used to compare two buffers.
It is used to compare two strings.
It compares the two strings in accordance to the current locale.
It compares a certain number of characters from two strings.
It is used to transform string using locale.
Sr.No | Functions & Description |
---|---|
1 | memcmp |
2 | strcmp |
3 | strcoll |
4 | strncmp |
5 | strxfrm |
Comparing two strings
Let's look at the following example, where we are going to compare the two strings.
#include <iostream> #include <cstring> int main() { char a[] = "Hi"; char b[] = "Hello"; int x = strcmp(a, b); if (x < 0) { std::cout << a << " is less than " << b << std::endl; } else if (x > 0) { std::cout << a << " is greater than " << b << std::endl; } else { std::cout << a << " is equal to " << b << std::endl; } return 0; }
Output
If we run the above code it will generate the following output −
Hi is greater than Hello
Searching
Searching refers to the process of finding a specific item in a collection of data. The commonly used C++ <cstring> searching functions are listed below along with their description.
It searches an array for the first occurrence of a character.
It is used to find the first occurrence of a character.
It returns the length of the maximum initial segment that consists of only the characters not found in another byte string.
It finds the first location of any character from a set of separators.
It is used to find the last occurrence of a character.
It returns the length of the maximum initial segment that consists of only the characters found in another byte string.
It is used to find the last occurrence of a character.
Sr.No | Functions & Description |
---|---|
1 | memchr |
2 | strchr |
3 | strcspn |
4 | strpbrk |
5 | strrchr |
6 | strspn |
7 | strstr |
Finding First Occurrence
Following is the example, where we are going to find the first occurrence of the 'L' character in the string.
#include <iostream> #include <cstring> int main() { char x[] = "TUTORIALSPOINT"; char y = 'L'; char * z = strchr(x, y); if (z) { std::cout << "Result : " << (z - x) << std::endl; } else { std::cout << "Character Not Found." << std::endl; } return 0; }
Output
Output of the above code is as follows −
Result : 7