Hello Strings

Hello Strings

Make a program that declares a std::string text with the value "Hello String!".

Use the member function std::string::size() to print the size (number of chars) of it.

Then re-assign text to contain Hi Text!.

Print the size again.

Hint

The size of text can be found by calling text.size().

Include the <string> header to use std::string.

Answer

#include <iostream>
#include <string>

int main() {
    std::string text = "Hello String!";
    std::cout << text.size(); // Print the size of the string

    text = "Hi Text!"; // Reassign text
    std::cout << " - "<< text.size(); // Print the new size
}

This program will output 13 - 8

Questions