...

/

Inside the String

Inside the String

Learn to access the individual elements of a string.

Collection

A collection is a general term used to group multiple values into a single unit. We use collections all the time in the real world―a book is a collection of pages, a train is a collection of train cars, and a wardrobe is a collection of clothes.

String

A string is a collection of characters, mostly treated as a single unit. A string is used to store text values like a name, address, message, etc.

Initializing the string

The following program demonstrates the string initialization:

Press + to interact
#include <iostream>
#include <string>
using namespace std;
int main()
{
string string1 = "The is a string.";
cout << string1 << endl;
return 0;
}

Note: We have to write #include <string> at line 2 to include a header file string that allows the use of string data type in the program.

In the above code:

  • At line 7, we initialize
...