Separating multiple first and/or last names in C
up vote
7
down vote
favorite
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.
c
New contributor
|
show 2 more comments
up vote
7
down vote
favorite
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.
c
New contributor
usestrtok
to split a string up using space as delimiter. Then you just have to find out, for each string, whether they're all uppercase or not. You can do that by looping over the string (until the null terminator at the end of the string) and useisupper
to find out whether the letter in question is uppercase.
– Blaze
2 days ago
1
Well you can if i am just making mock data, where the first names are not in all uppercase, and the last names are in uppercase
– Michael James
2 days ago
6
Read the full name. Iterate from the end and stop at the first lower case letter. At that point you can split the name into first and last names.
– P.W
2 days ago
1
see this article for the misconseptions we programmers usually have about names
– king_nak
2 days ago
@Lundin Even in the original edit of the question, this said "last names [are] in all capital letters". Please read the entire question before jumping to conclusions next time.
– Nic Hartley
2 days ago
|
show 2 more comments
up vote
7
down vote
favorite
up vote
7
down vote
favorite
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.
c
New contributor
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.
c
c
New contributor
New contributor
edited 2 days ago
JoSSte
82911231
82911231
New contributor
asked 2 days ago
Michael James
362
362
New contributor
New contributor
usestrtok
to split a string up using space as delimiter. Then you just have to find out, for each string, whether they're all uppercase or not. You can do that by looping over the string (until the null terminator at the end of the string) and useisupper
to find out whether the letter in question is uppercase.
– Blaze
2 days ago
1
Well you can if i am just making mock data, where the first names are not in all uppercase, and the last names are in uppercase
– Michael James
2 days ago
6
Read the full name. Iterate from the end and stop at the first lower case letter. At that point you can split the name into first and last names.
– P.W
2 days ago
1
see this article for the misconseptions we programmers usually have about names
– king_nak
2 days ago
@Lundin Even in the original edit of the question, this said "last names [are] in all capital letters". Please read the entire question before jumping to conclusions next time.
– Nic Hartley
2 days ago
|
show 2 more comments
usestrtok
to split a string up using space as delimiter. Then you just have to find out, for each string, whether they're all uppercase or not. You can do that by looping over the string (until the null terminator at the end of the string) and useisupper
to find out whether the letter in question is uppercase.
– Blaze
2 days ago
1
Well you can if i am just making mock data, where the first names are not in all uppercase, and the last names are in uppercase
– Michael James
2 days ago
6
Read the full name. Iterate from the end and stop at the first lower case letter. At that point you can split the name into first and last names.
– P.W
2 days ago
1
see this article for the misconseptions we programmers usually have about names
– king_nak
2 days ago
@Lundin Even in the original edit of the question, this said "last names [are] in all capital letters". Please read the entire question before jumping to conclusions next time.
– Nic Hartley
2 days ago
use
strtok
to split a string up using space as delimiter. Then you just have to find out, for each string, whether they're all uppercase or not. You can do that by looping over the string (until the null terminator at the end of the string) and use isupper
to find out whether the letter in question is uppercase.– Blaze
2 days ago
use
strtok
to split a string up using space as delimiter. Then you just have to find out, for each string, whether they're all uppercase or not. You can do that by looping over the string (until the null terminator at the end of the string) and use isupper
to find out whether the letter in question is uppercase.– Blaze
2 days ago
1
1
Well you can if i am just making mock data, where the first names are not in all uppercase, and the last names are in uppercase
– Michael James
2 days ago
Well you can if i am just making mock data, where the first names are not in all uppercase, and the last names are in uppercase
– Michael James
2 days ago
6
6
Read the full name. Iterate from the end and stop at the first lower case letter. At that point you can split the name into first and last names.
– P.W
2 days ago
Read the full name. Iterate from the end and stop at the first lower case letter. At that point you can split the name into first and last names.
– P.W
2 days ago
1
1
see this article for the misconseptions we programmers usually have about names
– king_nak
2 days ago
see this article for the misconseptions we programmers usually have about names
– king_nak
2 days ago
@Lundin Even in the original edit of the question, this said "last names [are] in all capital letters". Please read the entire question before jumping to conclusions next time.
– Nic Hartley
2 days ago
@Lundin Even in the original edit of the question, this said "last names [are] in all capital letters". Please read the entire question before jumping to conclusions next time.
– Nic Hartley
2 days ago
|
show 2 more comments
6 Answers
6
active
oldest
votes
up vote
5
down vote
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.
2
I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator0x1F
.
– pipe
2 days ago
@pipe: Excellent idea, in case someone has a tab in their name. But then what if someone has 0x1F UNIT SEPARATOR in their name‽
– Kundor
2 days ago
@pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
– Aconcagua
2 days ago
add a comment |
up vote
1
down vote
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
}
add a comment |
up vote
0
down vote
strspn
and strcspn
could be used to iterate through the string and locate the first word that is all uppercase.
#include <stdio.h>
#include <string.h>
int main( void) {
char line = "Michael Daniel VAN DOORNE";
char upper = "ABCDEFGHIJKLMNOPQURSTUVWXYZ";
int last = 0;
int start = 0;
int check = 0;
do {
last = start + check;
check += start + 1;
start = strspn ( line + check, upper);//span of characters that are uppercase
if ( ' ' != line[start + check]) {//is the next character a space
start = strcspn ( line + check, " ");//find the space
if ( line[start + check]) {//not the terminatins zero
start++;
}
}
} while ( line[start + check] && ' ' != line[start + check]);
printf ( "%sn", &line[last]);
return 0;
}
I believe this fails for people (like my grandfather) with a single character middle name
– BurnsBA
2 days ago
@BurnsBA yes a single letter not part of the last name is a problem.
– xing
2 days ago
There are also single character last names: en.wikipedia.org/wiki/O_(surname)
– BurnsBA
2 days ago
@BurnsBA the only solution I see isF. M. NAME
for first and middle name, follow the letter with a dot??
– xing
2 days ago
add a comment |
up vote
0
down vote
Assuming last names are always written in upper case, start reading the string from the end and see when you have your last lower case.
int i=strlen(buffer)-1;
while(!islower(buffer[i]) && i>0)
i--;
strncpy(firstName,buffer,i+1);
strcpy(lastName,&buffer[i+2]);
New contributor
add a comment |
up vote
0
down vote
Here's another solution.Read until there are two capitals after each other or a capital and a space. Then use pointer arithmetic to fill first name and lastname.
char name = "Michael Daniel VAN DOORNE";
char *p = name;
char firstname[100] = { 0 };
char lastname[100] = { 0 };
while (*p)
{
if (isupper(p[0]) && (isupper(p[1]) || p[1] == ' '))
{
strcpy(lastname, p);
strncpy(firstname, name, p - name - 1);
break;
}
p++;
}
add a comment |
up vote
0
down vote
If you're working with ASCII, here is a charset-specific trick that will help you:
#define TWOUPPER(c0, c1) (!((c0) & 32) && !((c1) & 32))
This will work even on single character last names since the null character will fail the 5th bit check, and single character middle names will not be taken as the last name since the following space will not succeed the test.
Works with the following test cases for me by comparing every two characters in the string and stopping on a match:
char test1[100] = "Otto VON BISMARK",
test2[100] = "Johannes Diderik VAN DER WAALS",
test3[100] = "Vincent VAN GOGH",
test4[100] = "Govind A B C D P"; // Only the "P" is counted as the last name here
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
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.
2
I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator0x1F
.
– pipe
2 days ago
@pipe: Excellent idea, in case someone has a tab in their name. But then what if someone has 0x1F UNIT SEPARATOR in their name‽
– Kundor
2 days ago
@pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
– Aconcagua
2 days ago
add a comment |
up vote
5
down vote
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.
2
I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator0x1F
.
– pipe
2 days ago
@pipe: Excellent idea, in case someone has a tab in their name. But then what if someone has 0x1F UNIT SEPARATOR in their name‽
– Kundor
2 days ago
@pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
– Aconcagua
2 days ago
add a comment |
up vote
5
down vote
up vote
5
down vote
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.
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.
answered 2 days ago
Aconcagua
11.1k32142
11.1k32142
2
I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator0x1F
.
– pipe
2 days ago
@pipe: Excellent idea, in case someone has a tab in their name. But then what if someone has 0x1F UNIT SEPARATOR in their name‽
– Kundor
2 days ago
@pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
– Aconcagua
2 days ago
add a comment |
2
I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator0x1F
.
– pipe
2 days ago
@pipe: Excellent idea, in case someone has a tab in their name. But then what if someone has 0x1F UNIT SEPARATOR in their name‽
– Kundor
2 days ago
@pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
– Aconcagua
2 days ago
2
2
I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator
0x1F
.– pipe
2 days ago
I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator
0x1F
.– pipe
2 days ago
@pipe: Excellent idea, in case someone has a tab in their name. But then what if someone has 0x1F UNIT SEPARATOR in their name‽
– Kundor
2 days ago
@pipe: Excellent idea, in case someone has a tab in their name. But then what if someone has 0x1F UNIT SEPARATOR in their name‽
– Kundor
2 days ago
@pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
– Aconcagua
2 days ago
@pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
– Aconcagua
2 days ago
add a comment |
up vote
1
down vote
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
}
add a comment |
up vote
1
down vote
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
}
add a comment |
up vote
1
down vote
up vote
1
down vote
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
}
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
}
edited 2 days ago
answered 2 days ago
static_cast
397314
397314
add a comment |
add a comment |
up vote
0
down vote
strspn
and strcspn
could be used to iterate through the string and locate the first word that is all uppercase.
#include <stdio.h>
#include <string.h>
int main( void) {
char line = "Michael Daniel VAN DOORNE";
char upper = "ABCDEFGHIJKLMNOPQURSTUVWXYZ";
int last = 0;
int start = 0;
int check = 0;
do {
last = start + check;
check += start + 1;
start = strspn ( line + check, upper);//span of characters that are uppercase
if ( ' ' != line[start + check]) {//is the next character a space
start = strcspn ( line + check, " ");//find the space
if ( line[start + check]) {//not the terminatins zero
start++;
}
}
} while ( line[start + check] && ' ' != line[start + check]);
printf ( "%sn", &line[last]);
return 0;
}
I believe this fails for people (like my grandfather) with a single character middle name
– BurnsBA
2 days ago
@BurnsBA yes a single letter not part of the last name is a problem.
– xing
2 days ago
There are also single character last names: en.wikipedia.org/wiki/O_(surname)
– BurnsBA
2 days ago
@BurnsBA the only solution I see isF. M. NAME
for first and middle name, follow the letter with a dot??
– xing
2 days ago
add a comment |
up vote
0
down vote
strspn
and strcspn
could be used to iterate through the string and locate the first word that is all uppercase.
#include <stdio.h>
#include <string.h>
int main( void) {
char line = "Michael Daniel VAN DOORNE";
char upper = "ABCDEFGHIJKLMNOPQURSTUVWXYZ";
int last = 0;
int start = 0;
int check = 0;
do {
last = start + check;
check += start + 1;
start = strspn ( line + check, upper);//span of characters that are uppercase
if ( ' ' != line[start + check]) {//is the next character a space
start = strcspn ( line + check, " ");//find the space
if ( line[start + check]) {//not the terminatins zero
start++;
}
}
} while ( line[start + check] && ' ' != line[start + check]);
printf ( "%sn", &line[last]);
return 0;
}
I believe this fails for people (like my grandfather) with a single character middle name
– BurnsBA
2 days ago
@BurnsBA yes a single letter not part of the last name is a problem.
– xing
2 days ago
There are also single character last names: en.wikipedia.org/wiki/O_(surname)
– BurnsBA
2 days ago
@BurnsBA the only solution I see isF. M. NAME
for first and middle name, follow the letter with a dot??
– xing
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
strspn
and strcspn
could be used to iterate through the string and locate the first word that is all uppercase.
#include <stdio.h>
#include <string.h>
int main( void) {
char line = "Michael Daniel VAN DOORNE";
char upper = "ABCDEFGHIJKLMNOPQURSTUVWXYZ";
int last = 0;
int start = 0;
int check = 0;
do {
last = start + check;
check += start + 1;
start = strspn ( line + check, upper);//span of characters that are uppercase
if ( ' ' != line[start + check]) {//is the next character a space
start = strcspn ( line + check, " ");//find the space
if ( line[start + check]) {//not the terminatins zero
start++;
}
}
} while ( line[start + check] && ' ' != line[start + check]);
printf ( "%sn", &line[last]);
return 0;
}
strspn
and strcspn
could be used to iterate through the string and locate the first word that is all uppercase.
#include <stdio.h>
#include <string.h>
int main( void) {
char line = "Michael Daniel VAN DOORNE";
char upper = "ABCDEFGHIJKLMNOPQURSTUVWXYZ";
int last = 0;
int start = 0;
int check = 0;
do {
last = start + check;
check += start + 1;
start = strspn ( line + check, upper);//span of characters that are uppercase
if ( ' ' != line[start + check]) {//is the next character a space
start = strcspn ( line + check, " ");//find the space
if ( line[start + check]) {//not the terminatins zero
start++;
}
}
} while ( line[start + check] && ' ' != line[start + check]);
printf ( "%sn", &line[last]);
return 0;
}
edited 2 days ago
answered 2 days ago
1,144178
1,144178
I believe this fails for people (like my grandfather) with a single character middle name
– BurnsBA
2 days ago
@BurnsBA yes a single letter not part of the last name is a problem.
– xing
2 days ago
There are also single character last names: en.wikipedia.org/wiki/O_(surname)
– BurnsBA
2 days ago
@BurnsBA the only solution I see isF. M. NAME
for first and middle name, follow the letter with a dot??
– xing
2 days ago
add a comment |
I believe this fails for people (like my grandfather) with a single character middle name
– BurnsBA
2 days ago
@BurnsBA yes a single letter not part of the last name is a problem.
– xing
2 days ago
There are also single character last names: en.wikipedia.org/wiki/O_(surname)
– BurnsBA
2 days ago
@BurnsBA the only solution I see isF. M. NAME
for first and middle name, follow the letter with a dot??
– xing
2 days ago
I believe this fails for people (like my grandfather) with a single character middle name
– BurnsBA
2 days ago
I believe this fails for people (like my grandfather) with a single character middle name
– BurnsBA
2 days ago
@BurnsBA yes a single letter not part of the last name is a problem.
2 days ago
@BurnsBA yes a single letter not part of the last name is a problem.
2 days ago
There are also single character last names: en.wikipedia.org/wiki/O_(surname)
– BurnsBA
2 days ago
There are also single character last names: en.wikipedia.org/wiki/O_(surname)
– BurnsBA
2 days ago
@BurnsBA the only solution I see is
F. M. NAME
for first and middle name, follow the letter with a dot??2 days ago
@BurnsBA the only solution I see is
F. M. NAME
for first and middle name, follow the letter with a dot??2 days ago
add a comment |
up vote
0
down vote
Assuming last names are always written in upper case, start reading the string from the end and see when you have your last lower case.
int i=strlen(buffer)-1;
while(!islower(buffer[i]) && i>0)
i--;
strncpy(firstName,buffer,i+1);
strcpy(lastName,&buffer[i+2]);
New contributor
add a comment |
up vote
0
down vote
Assuming last names are always written in upper case, start reading the string from the end and see when you have your last lower case.
int i=strlen(buffer)-1;
while(!islower(buffer[i]) && i>0)
i--;
strncpy(firstName,buffer,i+1);
strcpy(lastName,&buffer[i+2]);
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
Assuming last names are always written in upper case, start reading the string from the end and see when you have your last lower case.
int i=strlen(buffer)-1;
while(!islower(buffer[i]) && i>0)
i--;
strncpy(firstName,buffer,i+1);
strcpy(lastName,&buffer[i+2]);
New contributor
Assuming last names are always written in upper case, start reading the string from the end and see when you have your last lower case.
int i=strlen(buffer)-1;
while(!islower(buffer[i]) && i>0)
i--;
strncpy(firstName,buffer,i+1);
strcpy(lastName,&buffer[i+2]);
New contributor
New contributor
answered 2 days ago
Alex
294
294
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
Here's another solution.Read until there are two capitals after each other or a capital and a space. Then use pointer arithmetic to fill first name and lastname.
char name = "Michael Daniel VAN DOORNE";
char *p = name;
char firstname[100] = { 0 };
char lastname[100] = { 0 };
while (*p)
{
if (isupper(p[0]) && (isupper(p[1]) || p[1] == ' '))
{
strcpy(lastname, p);
strncpy(firstname, name, p - name - 1);
break;
}
p++;
}
add a comment |
up vote
0
down vote
Here's another solution.Read until there are two capitals after each other or a capital and a space. Then use pointer arithmetic to fill first name and lastname.
char name = "Michael Daniel VAN DOORNE";
char *p = name;
char firstname[100] = { 0 };
char lastname[100] = { 0 };
while (*p)
{
if (isupper(p[0]) && (isupper(p[1]) || p[1] == ' '))
{
strcpy(lastname, p);
strncpy(firstname, name, p - name - 1);
break;
}
p++;
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Here's another solution.Read until there are two capitals after each other or a capital and a space. Then use pointer arithmetic to fill first name and lastname.
char name = "Michael Daniel VAN DOORNE";
char *p = name;
char firstname[100] = { 0 };
char lastname[100] = { 0 };
while (*p)
{
if (isupper(p[0]) && (isupper(p[1]) || p[1] == ' '))
{
strcpy(lastname, p);
strncpy(firstname, name, p - name - 1);
break;
}
p++;
}
Here's another solution.Read until there are two capitals after each other or a capital and a space. Then use pointer arithmetic to fill first name and lastname.
char name = "Michael Daniel VAN DOORNE";
char *p = name;
char firstname[100] = { 0 };
char lastname[100] = { 0 };
while (*p)
{
if (isupper(p[0]) && (isupper(p[1]) || p[1] == ' '))
{
strcpy(lastname, p);
strncpy(firstname, name, p - name - 1);
break;
}
p++;
}
answered 2 days ago
Serve Laurijssen
5,50022353
5,50022353
add a comment |
add a comment |
up vote
0
down vote
If you're working with ASCII, here is a charset-specific trick that will help you:
#define TWOUPPER(c0, c1) (!((c0) & 32) && !((c1) & 32))
This will work even on single character last names since the null character will fail the 5th bit check, and single character middle names will not be taken as the last name since the following space will not succeed the test.
Works with the following test cases for me by comparing every two characters in the string and stopping on a match:
char test1[100] = "Otto VON BISMARK",
test2[100] = "Johannes Diderik VAN DER WAALS",
test3[100] = "Vincent VAN GOGH",
test4[100] = "Govind A B C D P"; // Only the "P" is counted as the last name here
add a comment |
up vote
0
down vote
If you're working with ASCII, here is a charset-specific trick that will help you:
#define TWOUPPER(c0, c1) (!((c0) & 32) && !((c1) & 32))
This will work even on single character last names since the null character will fail the 5th bit check, and single character middle names will not be taken as the last name since the following space will not succeed the test.
Works with the following test cases for me by comparing every two characters in the string and stopping on a match:
char test1[100] = "Otto VON BISMARK",
test2[100] = "Johannes Diderik VAN DER WAALS",
test3[100] = "Vincent VAN GOGH",
test4[100] = "Govind A B C D P"; // Only the "P" is counted as the last name here
add a comment |
up vote
0
down vote
up vote
0
down vote
If you're working with ASCII, here is a charset-specific trick that will help you:
#define TWOUPPER(c0, c1) (!((c0) & 32) && !((c1) & 32))
This will work even on single character last names since the null character will fail the 5th bit check, and single character middle names will not be taken as the last name since the following space will not succeed the test.
Works with the following test cases for me by comparing every two characters in the string and stopping on a match:
char test1[100] = "Otto VON BISMARK",
test2[100] = "Johannes Diderik VAN DER WAALS",
test3[100] = "Vincent VAN GOGH",
test4[100] = "Govind A B C D P"; // Only the "P" is counted as the last name here
If you're working with ASCII, here is a charset-specific trick that will help you:
#define TWOUPPER(c0, c1) (!((c0) & 32) && !((c1) & 32))
This will work even on single character last names since the null character will fail the 5th bit check, and single character middle names will not be taken as the last name since the following space will not succeed the test.
Works with the following test cases for me by comparing every two characters in the string and stopping on a match:
char test1[100] = "Otto VON BISMARK",
test2[100] = "Johannes Diderik VAN DER WAALS",
test3[100] = "Vincent VAN GOGH",
test4[100] = "Govind A B C D P"; // Only the "P" is counted as the last name here
edited 2 days ago
answered 2 days ago
Govind Parmar
6,68653053
6,68653053
add a comment |
add a comment |
Michael James is a new contributor. Be nice, and check out our Code of Conduct.
Michael James is a new contributor. Be nice, and check out our Code of Conduct.
Michael James is a new contributor. Be nice, and check out our Code of Conduct.
Michael James is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53540199%2fseparating-multiple-first-and-or-last-names-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
use
strtok
to split a string up using space as delimiter. Then you just have to find out, for each string, whether they're all uppercase or not. You can do that by looping over the string (until the null terminator at the end of the string) and useisupper
to find out whether the letter in question is uppercase.– Blaze
2 days ago
1
Well you can if i am just making mock data, where the first names are not in all uppercase, and the last names are in uppercase
– Michael James
2 days ago
6
Read the full name. Iterate from the end and stop at the first lower case letter. At that point you can split the name into first and last names.
– P.W
2 days ago
1
see this article for the misconseptions we programmers usually have about names
– king_nak
2 days ago
@Lundin Even in the original edit of the question, this said "last names [are] in all capital letters". Please read the entire question before jumping to conclusions next time.
– Nic Hartley
2 days ago