C++: Joining strings

Read a first name and a last name. Print the full name, and how many letters it has (the space does not count).

What to build

A program that joins two names and counts their letters.

Requirements

  • Read the two names into string variables.
  • Join them with a space using +.
  • Count the letters without counting the space.

Input

One line with two words: the first name and the last name.

Output

Full name: FIRST LAST (N letters)

Constraints

Each name is one word.

Example

Input:

Nadia Rahman

Output:

Full name: Nadia Rahman (11 letters)

Why it works

+ joins strings into a new one, and length() counts characters. The two names' lengths added together leave out the space.

Examples

Example 1 · Input
Nadia Rahman
Expected output
Full name: Nadia Rahman (11 letters)
Example 2 · Input
Al Amin
Expected output
Full name: Al Amin (6 letters)

Hints

Hint 1

first + " " + last joins three strings into a new one.

Hint 2

length() counts the characters; subtract the space, or add the two lengths.

Solution

Show one correct solution (try it yourself first)
#include <iostream>
#include <string>
using namespace std;

int main() {
    string first, last;
    cin >> first >> last;
    string full = first + " " + last;
    cout << "Full name: " << full << " (" << first.length() + last.length() << " letters)" << endl;
    return 0;
}

Lesson: From C to C++ →