A C-string is just an array of characters ending with a null character '\\0'
.
Syntax:
char name[6] = "Hello"; // size = 5 letters + '\\0'
Limitation: fixed size, manual management, fewer built-in functions.
std::string
(C++ Strings)Provided in <string>
.
Much easier to use → behaves like a dynamic array of characters.
Syntax:
string s = "Hello";
Length:
s.size()
or s.length()
Access characters:
s[0]
, s.at(2)
Concatenation:
s + " World"
Appending:
s.push_back('!');
(for the C-Style)
s += "!";
(for the std::string)
Substrings:
s.substr(start, length)
Comparison:
if(s1 == s2)
Search:
s.find("llo")
→ returns index or string::npos
if not found