Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating multiple first and/or last names in C

Tags:

c

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.

Example:

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.

like image 569
Emil Rasmussen Avatar asked Nov 29 '18 13:11

Emil Rasmussen


2 Answers

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.

like image 115
Aconcagua Avatar answered Oct 17 '22 14:10

Aconcagua


So knowing if it is part of a first name or last name is a bit of a challenge, but from the sound of it, you are in control of the data, so you can either lowercase the first name and capitalize the last or use some other method.

Breaking up the string, this is relatively easy by using strtok.

Making some assumptions that you are reading names line by line and stuffing them into buffer.

Use strtok to break buffer into "names".

char *token
token = strtok(buffer, " "); //note the second parameter is what you want to parse the array by
while(token != NULL)
{
    if(isupper(token[0]))
        //store that token into your struct (first or last name) allow for multiple
    else
        //store into the other 

    token = strtok(NULL, " "); //keep looping on string for names
}
like image 1
static_cast Avatar answered Oct 17 '22 14:10

static_cast