Hello everyone,
below is my code. I try to extract a token in the a string, but i get stuck at the next token function.
can anyone help me out with this problem.
thanks
the error i got after i compiled and ran is
subString.c(36) : error C2106: '=' : left operand must be l-value
#include<stdio.h>
#include<string.h>
#define SIZE 100
// Function Prototype
void getInput(char line[]);
int isDelimiter(char letter);
int skipDelimiters(int startPos);
int scanToken(int startPos);
char *nextToken(char token[]);
char *subString(char token[], char inputString[], int start, int cPos);
// Global Variable
char inputString[SIZE];
int maxPos;
// Starting program
int main(void)
{
//int len;
// int startPos = 0;
// int lasPos = 3;
char token[20];
getInput(inputString);
maxPos = strlen(inputString);
token = nextToken(inputString);
printf("length of array %d", strlen(inputString));
//printf("\n\n%s \n\n", subString(token, inputString, startPos, lasPos));
return 0;
}
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//
// subString
char *subString(char token[], char inputString[], int start, int cPos)
{
for(start; start < cPos; start++)
{
if(start < cPos)
{
token[start] = inputString[start];
}
}
token [start] = '\0';
return token;
}
//////////////////////////
//////////
//////////
//////////
/////////
// getInput function
//------------------------
----------
----------
--
void getInput(char line[])
{
int i = 0;
while(i < 81 && (line[i++] = getchar()) != '\n')
;
line[i] = '\0';
}
//////////////////////////
//////////
//////////
//////////
/////////
// skipDelimiters function
//------------------------
----------
----------
--
/*
skips delimiters starting from the specified
position. Return the index of the first
non-delimiter character at or after startPos
*/
int skipDelimiters(int startPos)
{
int position = startPos;
while(position < maxPos)
{
char c = inputString[position];
if(!isDelimiter(c))
{
break;
}
position++;
}
return position;
}
//////////////////////////
//////////
//////////
//////////
////////
// scanToken function
//------------------------
----------
----------
--
/*
Skips ahead from starPos and returns the index
of the next delimiter character encountered, or
maxPos if no such delimiter is found.
*/
int scanToken(int startPos)
{
int position = startPos;
while(position < maxPos)
{
char c = inputString[position];
if(isDelimiter(c))
{
break;
}
position++;
}
return position;
}
//////////////////////////
//////////
//////////
//////////
//////////
/
// nextToken function
//------------------------
----------
----------
--
/*
Return next token from the String.
*/
char *nextToken(char token[])
{
int currentPosition;
int start;
currentPosition = skipDelimiters(currentPosi
tion);
if(currentPosition >=maxPos)
{
return 0;
}
start = currentPosition;
currentPosition = scanToken(currentPosition)
;
token = subString(token, inputString, start, currentPosition);
return token;
}
//////////////////////////
//////////
//////////
//////////
//////////
//
//------------------------
----------
----------
--
int isDelimiter(char letter)
{
char delimiters[] = {' ', ',', '\r', '\n', 0};
int valid = 0;
int i = 0;
while(letter != delimiters[i])
{
valid = 0;
i++;
}
if(letter == delimiters[i])
{
valid = 1;
}
return valid ;
}