Separating multiple first and/or last names in C

  • A+

So I'm working on a small project whereas I want to take mock data and separate them into structs. But I was thinking of the issue of people with multiple first and/or last names.

I want to write first names like you would do (like "Michael") and last names in all capital letters (like "JAMESON").

But what if I'm reading a name like Michael Daniel VAN DOORNE, etc. I don't know how I'd be able to separate "Michael Daniel" as first name, and "VAN DOORNE" as the last name. I tried to separate by stopping at the first capital letter, but I am of course capitalizing the first letter in someone's first names as well.


I want to read Michael Daniel VAN DOORNE, and separate it into "Michael Daniel" as firstname, and "VAN DOORNE" as the surname.

sscanf(buffer, "%s %s", firstName, lastName); 

That wouldnt work naturally. But i am kinda stuck on coming up with a solution for mock names with multiple first and last names.


As you seem to be in total control of the data, I rather recommend a different approach:

A specific separator character in between forename(s) and surname(s). Then you don't rely on case sensitivity any more, especially the single character name issue appearing in another answer isn't an issue any more.

Separator character should be one that won't ever appear in any name, such as a tab (in contrast to space) character, #, '|', ... Even comma or semicolon should be fine, though the period might appear in abbreviated names and thus should not be used.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: