[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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;
}

//////////////////////////////////////////////////////////////////
0
thayboi3000
Asked:
thayboi3000
3 Solutions
 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now