133k views
0 votes
Write c++ function that receives input a person’s first name and surname, and then just displays the initials. For example: John Peter Joe, the initials JPJ must be displayed

User Cmag
by
6.9k points

1 Answer

6 votes

Answer:

void Initials(string firstName,string lastName)

{

cout<<firstName[0];

for(int i=1;i<firstName.length();i++)

{

if(firstName[i-1]==' ')

{

cout<<firstName[i];

}

}

cout<<lastName[0];

for(int i=1;i<lastName.length();i++)

{

if(lastName[i-1]==' ')

{

cout<<lastName[i];

}

}

cout<<endl;

}

Step-by-step explanation:

The above written code is the function Initials which prints the full name.Parameters provided in the function are firstname and lastname in the format of string.You should include string header file for this code to run.