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

book
Strings

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 +:

string a = "Hello ";
string b = "World";
string c = a + b;
cout << c;
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():

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

For finding the length of the line you can use the funtion .size(): a.size().

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

string a = "Hello ";
cout << a.append("everyone!");
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 the new line 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

dots
+
dots
*
dots
c.append()
dots
-
dots
b
dots
c
dots
a
dots
c.length()

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt