Link to home
Start Free TrialLog in
Avatar of thayboi3000
thayboi3000

asked on

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

//////////////////////////////////////////////////////////////////
Avatar of kledbetter
kledbetter

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
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial