30 likes | 156 Views
Problem Session. Working in pairs of two, solve the following problem. Problem. Using OCD, design and implement a function that, given a string containing a full name in First-Middle-Last order: John Quincy Doe returns a string containing that name in Last-First-MiddleInitial order:
E N D
Problem Session Working in pairs of two, solve the following problem...
Problem Using OCD, design and implement a function that, given a string containing a full name in First-Middle-Last order: John Quincy Doe returns a string containing that name in Last-First-MiddleInitial order: Doe, John Q.
Coding /* LastFirstMiddleInitial * Receive: name, a string. * Precondition: name contains a name in F-M-L order. * Return: the same name in L, F MI order. */ string LastFirstMiddleInitial(string name) { unsigned blank1Pos = name.find(‘ ‘, 0); string firstName = name.substr(0, blank1Pos); char middleInitial = name[blank1Pos+1]; unsigned blank2Pos = name.find(‘ ‘, blank1Pos+1); string lastName = name.substr(blank2Pos+1, name.size()-blank2Pos-1); return lastName + “, “ + firstName + ‘ ‘ + middleInitial + ‘.’; }