Thread: how to split a string into different parts?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Question how to split a string into different parts?

    Sample input string

    james 35 bond 30
    berry 40 bond 37

    The program must separte each input line into 4 parts. first name, age, last name, and age+7, the following code is a sample of with only 2 component, I don't know how to do it with 4 components. Could someone edit the code for me please.



    Code:
    void main(int argc, char *argv[])
    {
    	struct person list[];
    	int i,n;
    	char *ch, *ch1;
    	char buf[256];
    	FILE *file1;
    	i=0;
    	fgets(buf,250,file1);    
    
    	while(!feof(file1))
    	{
    	ch=strchr(buf,'\n');
    	if (ch!=NULL) *ch='\0';
    	else break;
    	ch = strchr(buf, ' ');
    	if(ch!=NULL) 
                     { 
                     *ch='\0'; 
                     ch1=++ch;  //age
                 
                      }
    	else break;
    	strcpy(list[i].lastname,buf);
                    list[i].age=atoi(ch1);
    	i++;		
    	fgets(buf,256,file1);
    	}
    
    	n=i;
    	for(i=0;i<n;i++)
                  printf("%s is %d years, first name is %s\n", list[i].lastname, list[i].age); 
        }

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    strtok or strsep will do this though strsep doesn't conform to C99 standard. A space ' ' delimiter would be used.

    http://www.manpagez.com/man/3/strtok/

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do not use feof to control loop - read FAQ
    you can use sscanf to parse the string into tokens
    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

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Quote Originally Posted by vart View Post
    do not use feof to control loop - read FAQ
    you can use sscanf to parse the string into tokens

    could you show me how you could do in the code above please.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    while(fgets(buf,sizeof buf,file1)    )
    {
    }
    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. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM