string class has been briefly introduced in an earlier chapter. It is a very powerful class to handle and manipulate strings of characters. However, because strings are, in fact, sequences of characters, we can represent them also as plain arrays of elements of a character type.1
char foo [20];
char. It can be represented as:"Hello" or the sequence "Merry Christmas" can be stored in foo, since both would fit in a sequence with a capacity for 20 characters.'0円' (backslash, zero).char called foo can be represented storing the character sequences "Hello" and "Merry Christmas" as:'0円') has been added in order to indicate the end of the sequence. The panels in gray color represent char elements with undetermined values.1
char myword[] = { 'H', 'e', 'l', 'l', 'o', '0円' };
char initialized with the characters that form the word "Hello" plus a null character '0円' at the end."). For example:1
"the result is: "
") are literal constants. And their type is, in fact, a null-terminated array of characters. This means that string literals always have a null character ('0円') automatically appended at the end.myword can be initialized with a null-terminated sequence of characters by either one of these two statements:1
2
char myword[] = { 'H', 'e', 'l', 'l', 'o', '0円' };
char myword[] = "Hello";
myword is declared with a size of 6 elements of type char: the 5 characters that compose the word "Hello", plus a final null character ('0円'), which specifies the end of the sequence and that, in the second case, when using double quotes (") it is appended automatically.1
2
myword = "Bye";
myword[] = "Bye";
1
myword = { 'B', 'y', 'e', '0円' };
1
2
3
4
myword[0] = 'B';
myword[1] = 'y';
myword[2] = 'e';
myword[3] = '0円';
string ), still, plain arrays with null-terminated sequences of characters (C-strings) are a natural way of representing strings in the language; in fact, string literals still always produce null-terminated character sequences, and not string objects.cin and cout support null-terminated sequences directly, allowing them to be directly extracted from cin or inserted into cout, just like strings. For example:// strings and NTCS:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char question1[] = "What is your name? ";
string question2 = "Where do you live? ";
char answer1 [80];
string answer2;
cout << question1;
cin >> answer1;
cout << question2;
cin >> answer2;
cout << "Hello, " << answer1;
cout << " from " << answer2 << "!\n";
return 0;
}
What is your name? Homer Where do you live? Greece Hello, Homer from Greece!
cin and cout, but there is a notable difference in their declarations: arrays have a fixed size that needs to be specified either implicit or explicitly when declared; question1 has a size of exactly 20 characters (including the terminating null-characters) and answer1 has a size of 80 characters; while strings are simply strings, no size is specified. This is due to the fact that strings have a dynamic size determined during runtime, while the size of arrays is determined on compilation, before the program runs.string's member functions c_str or data:1
2
3
4
char myntcs[] = "some text";
string mystring = myntcs; // convert c-string to string
cout << mystring; // printed as a library string
cout << mystring.c_str(); // printed as a c-string
c_str and data members of string are equivalent)