Write a fragment of code that reads a line of text (using getline) from standard input consisting of a last name followed by a first name (separated by exactly one blank), and prints out the first initial followed by a period then a blank then the full last name. Declare any necessary variables. (string processing)

LANGUAGE: C++

CHALLENGE:

Write a fragment of code that reads a line of text (using getline) from standard input consisting of a last name followed by a first name (separated by exactly one blank), and prints out the first initial followed by a period then a blank then the full last name. Declare any necessary variables. (string processing)

SOLUTION:



string firstName, lastName, Name;
int idx;

getline(cin,Name);
idx=Name.find(' ',0);

lastName=Name.substr(0,idx);
firstName=Name.substr(idx+1,Name.length()-idx);

cout<<firstName[0]<<". "<<lastName<<endl;