• C

Tokenizer in C?

below is my code, I try to copy a string into another string when i get first index position of the string and the last index of the string when it gets to the comman.  Unfortunately,  i do not get that idea
please help me out. I am new to C.

#define SIZE 81

void getInput(char line[]);
int isDelimiter(char c[]);

int maxPos;
char inputString[SIZE];
int newPos;

int main(void)
{      int i;
      char tempLastName[20];

      printf("Please enter your full name folllows this format: \n");
      printf("Last name, first name, V >> ");

    maxPos = strlen(inputString);
    newPos = isDelimiter(inputString);
      printf("newPos %d\n\n", newPos);

      for(i = 0; i < newPos; i++)
            //printf("%c", inputString[i]);
            if(i < newPos)
                  tempLastName[i] = inputString[i];

      printf("\n\nLast Name %s \n\n", tempLastName);
//      printf("%s\n\n", inputString);
//      printf("Max number of character inside the string is: %d\n\n", maxPos);

      return 0;


void getInput(char line[])
      int i = 0;
      while(i < 81 && (line[i++] = getchar()) != '\n')

      line[i] = '\0';



int isDelimiter(char c[])
      int count = 0;
      int i;
      for(i = 0; c[i] != ','; i++)

      return count;

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Everything looks fine, except for two things:

1.    tempLastName[newPos];   -- this line doesn't do anything.  Not sure what you were looking to do here.
2.   you forgot to put the zero terminator on the tempLastName after your "for" loop.

Change it to this:

     for(i = 0; i < newPos; i++)
          //printf("%c", inputString[i]);
          if(i < newPos)
               tempLastName[i] = inputString[i];
     tempLastName [i] = '\0';

and you'll be fine.

Use strtok function

An example of a strtok use

Also refer to the man page

Your code will go something like

char string[128] = "blah blah";
char delim[] = ","; <-- put all your delimiter characters here
int i = 1;
char * temp = strtok ( string, delim );

while ( temp )
         printf ( " token %d is %s", i, temp );
         temp = strtok (NULL, delim);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Actually, you dont need the 'if' in the loop either:

     for(i = 0; i < newPos; i++)
        tempLastName[i] = inputString[i];
     tempLastName [i] = '\0';

which can be further improved to:

   strncpy ( tempLastName, inputString, newPos );

... you probably should enhance the isDelimiter function to check for end of string, just in case there's no comma.
Otherwise it will scan past the end of the string and you'll get a lot of garbage or a KABOOM!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.