• 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.
thanks


#include<stdio.h>
#include<string.h>
#define SIZE 81



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


///// GLOBAL VARIABLES
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 >> ");

    getInput(inputString);
    maxPos = strlen(inputString);
    newPos = isDelimiter(inputString);
      tempLastName[newPos];
      
      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++)
      {      
          count++;
          
      }

      return count;
}

//////////////////////////////////////////////////////////////////
thayboi3000Asked:
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.

kledbetterCommented:
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.

-Keith
0
sunnycoderCommented:
Use strtok function

An example of a strtok use
http://alpha.osnippets.org/?content=snippet&id=318

Also refer to the man page
http://www.die.net/doc/linux/man/man3/strtok.3.html

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 );
         i++;
         temp = strtok (NULL, delim);
}
0

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
PaulCaswellCommented:
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 );

Paul
0
grg99Commented:
... 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!

0
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
C

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.