Thread: splitting up names (strings)

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    9

    splitting up names (strings)

    This code works for names which have 1 word lastnames, but not for 2 word last names (such as Van Damm). How can I get the whole last name?

    Code:
          while(fgets(line, sizeof(line), filelist)!=NULL){
             name=strtok(line,";");
             strcpy(nametmp,name);
             street=strtok(NULL,";");
             city=strtok(NULL,";");
             firstname=strtok(nametmp," ");
             lastname=strtok(NULL," ");
             printf("full name: %s\n",name);
             printf("first name: %s\n",firstname);
             printf("last name: %s\n",lastname);
             printf("street: %s\n",street);
          }
          fclose(filelist);
       }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Could you post an example of an input line?

    You could add some smarts and use functions other that strtok.
    Parsing a String into Tokens Using sscanf
    Parsing a String into Tokens Using strcspn 1
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    an input line would be this:
    John Van Damm;123 Fake Street;Coon Rapids


    the only toruble is breaking up the name

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include<stdio.h>
    
    int main( void )
    {
        char str[] = "John Van Damm;123 Fake Street;Coon Rapids";
    	char firstname[10];
    	char lastname[20];
    	char street[30];
    	char city [30];
    	sscanf(str,"%[^ ] %[^;];%[^;];%[^;]",firstname,lastname,street, city);
    
    
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    With input from fgets, the only minor mod I might make to vart's post is this:
    Code:
          if ( sscanf(str[i],"%[^ ] %[^;];%[^;];%[^;\n]",
                      firstname,lastname,street, city) == 4 )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Good point, thought about \n but looked at my code not at the original one... so ended with [^;]

    but if we look at the original then
    Code:
    sscanf(line,
    should be. str[i] is a little bit confusing
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by vart
    but if we look at the original then
    Code:
    sscanf(line,
    should be. str[i] is a little bit confusing
    Yeah, I cheated. That's why I didn't highlight it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    9
    thanks works like a charm

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788

    Thumbs up

    Quote Originally Posted by Dave_Sinkula
    You could add some smarts and use functions other that strtok.
    Parsing a String into Tokens Using sscanf
    Parsing a String into Tokens Using strcspn 1
    .
    Ah! And thanks for the links. I have learned something new about printf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. splitting strings...
    By newbie_socketsp in forum C Programming
    Replies: 4
    Last Post: 08-15-2008, 10:17 AM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. an array of names (strings)
    By orion- in forum C++ Programming
    Replies: 7
    Last Post: 09-29-2005, 12:54 PM
  4. Splitting strings into words.
    By 0x7f in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2003, 03:49 PM
  5. splitting strings
    By Unreg1 in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2002, 11:02 PM