162k views
5 votes
Which statements will print the length (number of characters) of the variable name given the following statement?

string name = ""Jane Doe"";

A) cout << name.size();
B) cout << length(name);
C) cout << name.length;
D) cout << size(name);

User Hirvesh
by
8.6k points

1 Answer

4 votes

Final answer:

The correct statement to print the length of the variable name 'Jane Doe' is 'cout << name.size();' A similar statement that would also work is 'cout << name.length();' as both return the length of the string.

Step-by-step explanation:

You asked which statements will print the length (number of characters) of the variable name given the following statement: string name = "Jane Doe";.

The correct statement to use would be:

  • A) cout << name.size();

Additionally, another correct statement that is not listed in your options but would also work is:

  • cout << name.length();

Here is a brief explanation of why these two are correct:

  1. The .size() method returns the number of characters in a string variable.
  2. The .length() method does the same, hence both A) cout << name.size(); and cout << name.length(); will provide the desired output.

Options B), C), and D) do not use the correct syntax for retrieving the length of a string in C++.

User Sergey Ratnikov
by
7.6k points