In C++, there are multiple ways to represent strings. In this article, we will discuss strings represented as one-dimensional arrays with elements of type char, a representation that comes from the C language. These strings are also called null-terminated byte strings (NTBS). Internally, after the last valid character (byte) in the string, there is the '\0' character – the character with ASCII code 0, also called the null character. Thus, to represent the word "copil" (child) in C/C++, which has 5 characters, 6 bytes will be used, with the values: 'c', 'o', 'p', 'i', 'l', '\0'.
A character array is declared in C++ as follows:
char s[11];
A string has been declared that can store a maximum of 11 characters, with indices 0 1 ... 10. The array s can store at most 10 useful characters, with the '\0' character stored after the last useful character.
Also, a string can be initialized at the time of declaration. The following examples declare and initialize character arrays with the string "copil":
char s[11] = "copil"; // only 6 characters are used
char t[]="child"; // automatically allocates 6 bytes for the array t: the 5 letters and the null character \0
char x[6]={'c','h','i','l','d','\0'}; // initialization is similar to any array - character arrays are arrays
char z[]={'c','h','i','l','d','\0'}; // automatically allocates 6 bytes for the array
This can be done with the insertion operator << into a stream:
cout << s << endl;
The extraction operator >> from a stream can be used:
cin >> s;
In this way, due to the specific behavior of the >> operator, strings containing spaces cannot be read – characters will be read up to the first space, without including it.
To read strings that contain spaces, we can use the getline method of the cin object or another istream object:
istream& getline (char* s, streamsize n );
Characters from the input stream (keyboard) will be read into the array s until the newline character '\n' is encountered, but not more than n-1 characters. The '\n' character will not be added to the array s but will be extracted from the stream. For example:
cin.getline(s , 11);
We could say that getline reads the entire line and skips ENTER. Here is a complete example:
#include
using namespace std;
int main(){
char name1[31], name2[31];
cout << "What is your name? (surname, first name) ";
cin.getline(name1, 31);
cout << "What is your friend's name? ";
cin.getline(name2 , 31);
cout << "Your name is " << name1 << endl;
cout << "You are friends with " << name2 << endl;
return 0;
}
Another way to read a string that may contain spaces is to use the get method of the istream object, which we will not present here.
Since character arrays are actually arrays, the [] operator is used to reference a character in the array, as in the following example:
char s[]="abac"; // the array consists of 5 characters: the 4 letters and the null character '\0'
cout << s[3]; // c
s[1] = 'r';
cout << s; // arac
cout << s[10]; // ??? unpredictable behavior: there is no character with index 10 in the array
In many situations, it is necessary to analyze each character in the array. To do this, the array must be iterated through; this is done similarly to iterating through any array. The difference is that, for a character array, the length is not explicitly known. It can be determined with the strlen function (see below), but we can control the iteration by knowing that the null character '\0' appears after the last valid character in the array.
The following examples iterate through a character array and display the characters separated by spaces:
char s[11];
cin >> s; // reads a word without spaces
int i = 0;
while(s[i] != '\0')
{
cout << s[i] << " ";
i ++;
}
or more concisely:
char s[11];
cin >> s; // reads a word without spaces
for(int i = 0 ; s[i] ; i ++)
cout << s[i] << " ";
In this section you can generate a summary of the page content using AI! Feel free to use the button below whenever you are in a hurry and don't have time to learn everything!
In this section you can ask our expert robot anything related to the questions you encountered during the lessons! Feel free to use the button below whenever you need additional explanations!