Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Strings | Variables
Introduction to C++ | Mobile-Friendly

bookStrings

As we mentioned before strings are used for storing text. Let’s explore some useful functions using this type of variables.

To concatenate 2 (or more) lines to store the result in the new variable, use the operator +:

1234
string a = "Hello "; string b = "World"; string c = a + b; cout << c;
copy

String variables contain functions that help perform certain operations on them.

For example, you can find the length of the string variable using the built-in function .length():

12
string a = "Hello user!"; cout << a.length();
copy

You can also concatenate strings using the built-in function .append():

12
string a = "Hello "; cout << a.append("everyone!");
copy
question-icon

Concatenate two lines (a and b) and store the result in the variable c. Find the length of this new variable. Print c and its length:

string a = "I love C++! ";
string b = "I love programming!";
string c = a
b;

cout <<
<< endl;
cout <<
;
I love C++! I love programming!
31

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Ask me questions about this topic

Summarize this chapter

Show real-world examples

bookStrings

Swipe to show menu

As we mentioned before strings are used for storing text. Let’s explore some useful functions using this type of variables.

To concatenate 2 (or more) lines to store the result in the new variable, use the operator +:

1234
string a = "Hello "; string b = "World"; string c = a + b; cout << c;
copy

String variables contain functions that help perform certain operations on them.

For example, you can find the length of the string variable using the built-in function .length():

12
string a = "Hello user!"; cout << a.length();
copy

You can also concatenate strings using the built-in function .append():

12
string a = "Hello "; cout << a.append("everyone!");
copy
question-icon

Concatenate two lines (a and b) and store the result in the variable c. Find the length of this new variable. Print c and its length:

string a = "I love C++! ";
string b = "I love programming!";
string c = a
b;

cout <<
<< endl;
cout <<
;
I love C++! I love programming!
31

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
some-alt