-3

So I have a lab problem I need help with. I need to write a function that has a name parameter that includes the person's entire name. Ex) "John Quincy Doe". From that input, my function has to return the last name with a comma, then the first name and middle initial. Ex) "Doe, John Q". I have the right idea of checking for space and then breaking them into substrings, but how do I go about doing that in C++?

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63

2 Answers2

2

That is commonly called tokenizing a string. There are multiple answers on here. I would just comment, but apparently I can't until I have 50 reputation.

C++ Tokenize String

How do I tokenize a string in C++?

Community
  • 1
  • 1
Pat
  • 51
  • 1
  • 2
  • 9
0

Depending on whether you are allowed to use it, you could also implement your function with a regular expression that matches three parts of the name and uses the sub matches in the correct order resp. with only the first letter of the second match, e.g. the middle name.

blackbird
  • 957
  • 12
  • 18