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.










share|improve this question









New contributor




Emil Rasmussen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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
    Nov 29 at 13:34






  • 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
    – Emil Rasmussen
    Nov 29 at 13:37






  • 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
    Nov 29 at 13:49






  • 1




    see this article for the misconseptions we programmers usually have about names
    – king_nak
    Nov 29 at 14:18










  • @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
    Nov 29 at 18:20















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.










share|improve this question









New contributor




Emil Rasmussen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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
    Nov 29 at 13:34






  • 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
    – Emil Rasmussen
    Nov 29 at 13:37






  • 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
    Nov 29 at 13:49






  • 1




    see this article for the misconseptions we programmers usually have about names
    – king_nak
    Nov 29 at 14:18










  • @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
    Nov 29 at 18:20













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.










share|improve this question









New contributor




Emil Rasmussen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Emil Rasmussen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Emil Rasmussen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 29 at 13:41









JoSSte

82911331




82911331






New contributor




Emil Rasmussen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 29 at 13:30









Emil Rasmussen

362




362




New contributor




Emil Rasmussen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Emil Rasmussen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Emil Rasmussen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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
    Nov 29 at 13:34






  • 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
    – Emil Rasmussen
    Nov 29 at 13:37






  • 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
    Nov 29 at 13:49






  • 1




    see this article for the misconseptions we programmers usually have about names
    – king_nak
    Nov 29 at 14:18










  • @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
    Nov 29 at 18:20


















  • 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
    Nov 29 at 13:34






  • 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
    – Emil Rasmussen
    Nov 29 at 13:37






  • 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
    Nov 29 at 13:49






  • 1




    see this article for the misconseptions we programmers usually have about names
    – king_nak
    Nov 29 at 14:18










  • @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
    Nov 29 at 18:20
















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
Nov 29 at 13:34




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
Nov 29 at 13:34




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
– Emil Rasmussen
Nov 29 at 13:37




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
– Emil Rasmussen
Nov 29 at 13:37




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
Nov 29 at 13:49




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
Nov 29 at 13:49




1




1




see this article for the misconseptions we programmers usually have about names
– king_nak
Nov 29 at 14:18




see this article for the misconseptions we programmers usually have about names
– king_nak
Nov 29 at 14:18












@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
Nov 29 at 18:20




@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
Nov 29 at 18:20












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.






share|improve this answer

















  • 2




    I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator 0x1F.
    – pipe
    Nov 29 at 16:58










  • @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
    Nov 29 at 17:59










  • @pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
    – Aconcagua
    Nov 30 at 0:28


















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
}





share|improve this answer






























    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;
    }





    share|improve this answer























    • I believe this fails for people (like my grandfather) with a single character middle name
      – BurnsBA
      Nov 29 at 14:12










    • @BurnsBA yes a single letter not part of the last name is a problem.
      – xing
      Nov 29 at 14:13










    • There are also single character last names: en.wikipedia.org/wiki/O_(surname)
      – BurnsBA
      Nov 29 at 14:14










    • @BurnsBA the only solution I see is F. M. NAME for first and middle name, follow the letter with a dot??
      – xing
      Nov 29 at 14:19


















    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]);





    share|improve this answer








    New contributor




    Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      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++;
      }





      share|improve this answer




























        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





        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Emil Rasmussen is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          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

























          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.






          share|improve this answer

















          • 2




            I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator 0x1F.
            – pipe
            Nov 29 at 16:58










          • @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
            Nov 29 at 17:59










          • @pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
            – Aconcagua
            Nov 30 at 0:28















          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.






          share|improve this answer

















          • 2




            I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator 0x1F.
            – pipe
            Nov 29 at 16:58










          • @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
            Nov 29 at 17:59










          • @pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
            – Aconcagua
            Nov 30 at 0:28













          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 29 at 14:22









          Aconcagua

          11.3k32142




          11.3k32142








          • 2




            I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator 0x1F.
            – pipe
            Nov 29 at 16:58










          • @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
            Nov 29 at 17:59










          • @pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
            – Aconcagua
            Nov 30 at 0:28














          • 2




            I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator 0x1F.
            – pipe
            Nov 29 at 16:58










          • @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
            Nov 29 at 17:59










          • @pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
            – Aconcagua
            Nov 30 at 0:28








          2




          2




          I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator 0x1F.
          – pipe
          Nov 29 at 16:58




          I suggest using one of the standard ASCII characters designed for this purpose, for example the unit separator 0x1F.
          – pipe
          Nov 29 at 16:58












          @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
          Nov 29 at 17:59




          @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
          Nov 29 at 17:59












          @pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
          – Aconcagua
          Nov 30 at 0:28




          @pipe They are fine for file processing, but less suitable if input file is written by hand with ordinary text editor...
          – Aconcagua
          Nov 30 at 0:28












          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
          }





          share|improve this answer



























            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
            }





            share|improve this answer

























              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
              }





              share|improve this answer














              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
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 29 at 14:24

























              answered Nov 29 at 14:15









              static_cast

              397314




              397314






















                  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;
                  }





                  share|improve this answer























                  • I believe this fails for people (like my grandfather) with a single character middle name
                    – BurnsBA
                    Nov 29 at 14:12










                  • @BurnsBA yes a single letter not part of the last name is a problem.
                    – xing
                    Nov 29 at 14:13










                  • There are also single character last names: en.wikipedia.org/wiki/O_(surname)
                    – BurnsBA
                    Nov 29 at 14:14










                  • @BurnsBA the only solution I see is F. M. NAME for first and middle name, follow the letter with a dot??
                    – xing
                    Nov 29 at 14:19















                  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;
                  }





                  share|improve this answer























                  • I believe this fails for people (like my grandfather) with a single character middle name
                    – BurnsBA
                    Nov 29 at 14:12










                  • @BurnsBA yes a single letter not part of the last name is a problem.
                    – xing
                    Nov 29 at 14:13










                  • There are also single character last names: en.wikipedia.org/wiki/O_(surname)
                    – BurnsBA
                    Nov 29 at 14:14










                  • @BurnsBA the only solution I see is F. M. NAME for first and middle name, follow the letter with a dot??
                    – xing
                    Nov 29 at 14:19













                  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;
                  }





                  share|improve this answer














                  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;
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 29 at 14:12

























                  answered Nov 29 at 14:10









                  xing

                  1,144178




                  1,144178












                  • I believe this fails for people (like my grandfather) with a single character middle name
                    – BurnsBA
                    Nov 29 at 14:12










                  • @BurnsBA yes a single letter not part of the last name is a problem.
                    – xing
                    Nov 29 at 14:13










                  • There are also single character last names: en.wikipedia.org/wiki/O_(surname)
                    – BurnsBA
                    Nov 29 at 14:14










                  • @BurnsBA the only solution I see is F. M. NAME for first and middle name, follow the letter with a dot??
                    – xing
                    Nov 29 at 14:19


















                  • I believe this fails for people (like my grandfather) with a single character middle name
                    – BurnsBA
                    Nov 29 at 14:12










                  • @BurnsBA yes a single letter not part of the last name is a problem.
                    – xing
                    Nov 29 at 14:13










                  • There are also single character last names: en.wikipedia.org/wiki/O_(surname)
                    – BurnsBA
                    Nov 29 at 14:14










                  • @BurnsBA the only solution I see is F. M. NAME for first and middle name, follow the letter with a dot??
                    – xing
                    Nov 29 at 14:19
















                  I believe this fails for people (like my grandfather) with a single character middle name
                  – BurnsBA
                  Nov 29 at 14:12




                  I believe this fails for people (like my grandfather) with a single character middle name
                  – BurnsBA
                  Nov 29 at 14:12












                  @BurnsBA yes a single letter not part of the last name is a problem.
                  – xing
                  Nov 29 at 14:13




                  @BurnsBA yes a single letter not part of the last name is a problem.
                  – xing
                  Nov 29 at 14:13












                  There are also single character last names: en.wikipedia.org/wiki/O_(surname)
                  – BurnsBA
                  Nov 29 at 14:14




                  There are also single character last names: en.wikipedia.org/wiki/O_(surname)
                  – BurnsBA
                  Nov 29 at 14:14












                  @BurnsBA the only solution I see is F. M. NAME for first and middle name, follow the letter with a dot??
                  – xing
                  Nov 29 at 14:19




                  @BurnsBA the only solution I see is F. M. NAME for first and middle name, follow the letter with a dot??
                  – xing
                  Nov 29 at 14:19










                  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]);





                  share|improve this answer








                  New contributor




                  Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






















                    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]);





                    share|improve this answer








                    New contributor




                    Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.




















                      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]);





                      share|improve this answer








                      New contributor




                      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      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]);






                      share|improve this answer








                      New contributor




                      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      share|improve this answer



                      share|improve this answer






                      New contributor




                      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      answered Nov 29 at 14:15









                      Alex

                      294




                      294




                      New contributor




                      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





                      New contributor





                      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






















                          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++;
                          }





                          share|improve this answer

























                            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++;
                            }





                            share|improve this answer























                              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++;
                              }





                              share|improve this answer












                              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++;
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 29 at 14:22









                              Serve Laurijssen

                              5,50022353




                              5,50022353






















                                  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





                                  share|improve this answer



























                                    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





                                    share|improve this answer

























                                      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





                                      share|improve this answer














                                      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






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 29 at 15:00

























                                      answered Nov 29 at 14:46









                                      Govind Parmar

                                      6,69653053




                                      6,69653053






















                                          Emil Rasmussen is a new contributor. Be nice, and check out our Code of Conduct.










                                          draft saved

                                          draft discarded


















                                          Emil Rasmussen is a new contributor. Be nice, and check out our Code of Conduct.













                                          Emil Rasmussen is a new contributor. Be nice, and check out our Code of Conduct.












                                          Emil Rasmussen 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.




                                          draft saved


                                          draft discarded














                                          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





















































                                          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







                                          Popular posts from this blog

                                          AnyDesk - Fatal Program Failure

                                          How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

                                          QoS: MAC-Priority for clients behind a repeater