C++ A person\'s initials is the first letter of their first name, followed by a
ID: 3846906 • Letter: C
Question
C++
A person's initials is the first letter of their first name, followed by a period, followed by the first letter of their last name, followed by a period. For example, the initials for David Turner are D.T.
Write a program that prompts the user for a first name and then prompt the user for a last name. Have the program display the user's initials.
As part of your solution, define a function named initials, which takes 2 arguments: the first name and the last name. The function returns the person's initials as a string.
Because the function arguments don't need to be changed and are complex data types, you should pass them into the function as const references.
Hint: use the substr function or the index operator of the string data type.
Explanation / Answer
#include<iostream.h>
#include<string.h>
void main()
{
String first_name;
String last_name;
cout<<"Enter Firstname ";
cin>>first_name;
cout<<"Enter Lastname ";
cin>>last_name;
String initial=initials(first_name,last_name);
cout<<"Initials :"<<initial;
}
String initials(String &fname,String &lname)
{
String finitial=fname.substr (0,1);
String linitial=lname.substr (0,1);
strcat (finitial,".");
strcat (finitial,linitial);
return finitial;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.