![]() |
VOOZH | about |
In C, a string is a character array terminated by a null character \0, making its size one more than the number of characters. C++ also supports this, and the compiler automatically adds \0 when initializing the array.
Initializing a String in C++:
1. char str[] = "Geeks";
2. char str[6] = "Geeks";
3. char str[] = {'G', 'e', 'e', 'k', 's', '\0'};
4. char str[6] = {'G', 'e', 'e', 'k', 's', '\0'};
Memory representation of a string "Geeks" in C++.
👁 Image
C-style strings rely on a number of functions defined in the <cstring> header. Let's explore three common functions:
The strlen function calculates the length of a string by counting characters until it encounters \0.
3 3
Explanation:
The strcmp function compares two strings lexicographically:
Greater
Explanation:
The strcpy function copies the content of one string into another.
gfg
Explanation: