splitting up names (strings)

This is a discussion on splitting up names (strings) within the C Programming forums, part of the General Programming Boards category; This code works for names which have 1 word lastnames, but not for 2 word last names (such as Van ...

  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,006
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473

    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 07: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, 02:49 PM
  5. splitting strings
    By Unreg1 in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2002, 10:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21