cpp substring 1

cpp substring

#include 
#include 

using namespace std;

void cpp_strings()
{
    string unformatted_full_name { "StephenHawking" };

    string first_name = unformatted_full_name.substr(0, 7);
    // string first_name { unformatted_full_name, 0, 7 };
    string last_name = unformatted_full_name.substr(7, 7);
    string formatted_full_name = first_name + last_name;

    formatted_full_name.insert(7, " ");
    cout << formatted_full_name;
}

int main()
{
    cpp_strings();
    return 0;
}

Here is what the above code is Doing:
1. We create a string variable called unformatted_full_name and assign it the value "StephenHawking".
2. We create a string variable called first_name and assign it the value of the first 7 characters of unformatted_full_name.
3. We create a string variable called last_name and assign it the value of the last 7 characters of unformatted_full_name.
4. We create a string variable called formatted_full_name and assign it the value of first_name + last_name.
5. We insert a space into formatted_full_name at index 7.
6. We print formatted_full_name to the console.

Similar Posts