In this chapter we are going to explore the basics of strings as well as introduce functions and errors.
Strings
A string is another datatype in C++. A string is a sequence of characters. Examples of strings are "Hello World!", "I am learning C++", etc. Even "423" is a string, if we write inside double quotation marks.
It is part of the Standard Library in C++. Standard Library is a collection of some libraries which contains functions and data types to perform some tasks. For string, we use the <string> library.
type=codeblock|id=cpp_string|autocreate=cpp#include <iostream>#include <string> // library for stringsusing namespace std;int main() {string a = "I am learning C++" ;cout << a << endl;return 0;}
The <string> library is included in the <iostream> library, so you don't need to include <string> separately if you already use <iostream>. However, it is recommended practice to include the library directly if you are using it.