Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse a string 1

Status
Not open for further replies.

cbeganesh

Programmer
Aug 3, 2005
5
US
Hello
I have a string in this format
lastname.firstname.middlename:
I want to parse these elements into separate strings
If i get any examples or sample code, It will be of great help

Thank You
 
Try strtok - the following code is without any error checking
Code:
char *instring = "last.first.middle";
char *last, *first, *middle;

last = strtok ( instring, "." );
first = strtok ( NULL, "." );
middle = strtok ( NULL, "." );
as ever use man first!

Columb Healy
 
im getting access violation error when i run that
 
This code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
main ()
 
{
 
char *instring = "last.first.middle";
char *last, *first, *middle;
 
last = strtok ( instring, "." );
first = strtok ( NULL, "." );
middle = strtok ( NULL, "." );
printf ( "Middle = %s\nLast = %s\nFirst = %s\n", middle, last, first ); 
}
compiles and runs as expected


Columb Healy
 
Im still getting the error, Im running this on VMS
but when i change the instring to char instring[100]
i dont get the error
 
I got to do it the hard way

Code:
char array [3][25];
    
void separate_string (char *string, char c)
{
    int length = strlen (string);
    int x, y = 0;
    int z = 0;
    char buff1 [25];

    for (x = 0; x < length; x++)
    {
        if (string [x + 1] == NULL)
        {
            buff1 [z] =  string [x];
            buff1 [z + 1] =  NULL;

            if (strlen (buff1))                
                strcpy (array [y], buff1);
        }
        else if (string [x] != c)
        {
            buff1 [z] =  string [x];
            z++;
                        
            if (string [x + 1] == c)
            {
                buff1 [z] = NULL;
                z = 0;
                if (strlen (buff1))
                {
                    strcpy (array [y], buff1);
                    y++;
                }
            }
        }
    }
}

printf ("\s", array [0]);
printf ("\s", array [1]);
printf ("\s", array [2]);

TC
 
I might have done it like this.
Code:
#include <stdio.h>

int main(void)
{
   const char line[] = "last name.first name.middle name";
   char last[32], first[20], middle[12];
   if(sscanf(line, "%31[^.].%19[^.].%11[^.]", last, first, middle) == 3)
   {
      printf("first  = \"%s\"\n", first);
      printf("middle = \"%s\"\n", middle);
      printf("last   = \"%s\"\n", last);
   }
   return 0;
}

/* my output
first  = "first name"
middle = "middle name"
last   = "last name"
*/
 
Never use strtok in this context:
Code:
char* p = "string literal";
...strtok(p,...
String literals in C/C++ are constants but strtok modifies its argument.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top