127k views
2 votes
C++ CHALLENGE ACTIVITY 2.15.1: Reading and outputting strings. Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is: Maya Jones Jones, Maya.

User Barry Last
by
8.1k points

1 Answer

2 votes

Answer:

Following are the program is given below

#include <iostream> // header file

#include<string> // header file

using namespace std; // namespace

int main() // main function

{

string fname,lname; // variable declaration

cin>>fname>>lname; // Read the input by user

cout<<lname; // print last name

cout<<", "; // display comma

cout<<fname<<endl; // display first name

return 0;

}

Output:

Maya Jones

Jones, Maya

Step-by-step explanation:

Following are the description of program .

  • Declared a variable "fname" and the "lname" as the string datatype .
  • Read the input by user in the "fname" and the "lname" variable by using cin function in c++.
  • After that print the "lname" variable.
  • then print the comma and finally print the "fname" variable
User Mbert
by
8.4k points