S
Stu Cazzo
Hi all,
I have a question on why strtok is doing what it's doing for my
splitString( string2 ); call.
Below is the output for the entire program:
token was: word1
token was: word2
token was: word3
token was: word1
token was: word3
empty field found - token <(null)>
The splitString( string1 ); works as expected, 3 tokens are found.
The splitString( string2 ); does not work as I expected.
I was expecting this:
token was: word1
empty field found - token <(null)>
token was: word3
Why does it not see the empty field for lineToken2?
Is there a better way to strip out the tokens for the case I have
where
there is no space between my delimiter?
-------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void splitString( char *string )
{
const char lineDelimiter[] = ",";
char *lineToken1;
char *lineToken2;
char *lineToken3;
lineToken1 = strtok( string, lineDelimiter );
if ( lineToken1 == '\0' )
{
printf("empty field found - token <%s>\n", lineToken1);
}
else
{
printf("token was: %s\n", lineToken1);
}
lineToken2 = strtok( NULL, lineDelimiter );
if ( lineToken2 == '\0' )
{
printf("empty field found - token <%s>\n", lineToken2);
}
else
{
printf("token was: %s\n", lineToken2);
}
lineToken3 = strtok( NULL, lineDelimiter );
if ( lineToken3 == '\0' )
{
printf("empty field found - token <%s>\n", lineToken3);
}
else
{
printf("token was: %s\n", lineToken3);
}
}
int main (int argc, const char **argv)
{
char string1[] = "word1,word2,word3";
char string2[] = "word1,,word3";
splitString( string1 );
splitString( string2 );
return( 1 );
}
I have a question on why strtok is doing what it's doing for my
splitString( string2 ); call.
Below is the output for the entire program:
token was: word1
token was: word2
token was: word3
token was: word1
token was: word3
empty field found - token <(null)>
The splitString( string1 ); works as expected, 3 tokens are found.
The splitString( string2 ); does not work as I expected.
I was expecting this:
token was: word1
empty field found - token <(null)>
token was: word3
Why does it not see the empty field for lineToken2?
Is there a better way to strip out the tokens for the case I have
where
there is no space between my delimiter?
-------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void splitString( char *string )
{
const char lineDelimiter[] = ",";
char *lineToken1;
char *lineToken2;
char *lineToken3;
lineToken1 = strtok( string, lineDelimiter );
if ( lineToken1 == '\0' )
{
printf("empty field found - token <%s>\n", lineToken1);
}
else
{
printf("token was: %s\n", lineToken1);
}
lineToken2 = strtok( NULL, lineDelimiter );
if ( lineToken2 == '\0' )
{
printf("empty field found - token <%s>\n", lineToken2);
}
else
{
printf("token was: %s\n", lineToken2);
}
lineToken3 = strtok( NULL, lineDelimiter );
if ( lineToken3 == '\0' )
{
printf("empty field found - token <%s>\n", lineToken3);
}
else
{
printf("token was: %s\n", lineToken3);
}
}
int main (int argc, const char **argv)
{
char string1[] = "word1,word2,word3";
char string2[] = "word1,,word3";
splitString( string1 );
splitString( string2 );
return( 1 );
}