okay for some reason I can't edit my other thread so I'll just make another one. Say I'm using this strtok, it runs like it should but I want it to print out the last string instead of the first when i do a printf at the end.

char *strtok( char *str1, const char *str2 );
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
    printf( "result is \"%s\"\n", result );
    result = strtok( NULL, delims );
}
printf("%s\n", str);

So the output of this is:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"

then the last printf shows: "now"
when i would like it to show "aid of their country"

any help?
oh and when i do printf('%s\n", result);
it comes out NULL

Try ending your string with your "token" - this from onlinepubs:
The strtok() function then searches from there for a byte that is contained in the current separator string. If no such byte is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token shall return a null pointer

I think that means it wraps the string.

Perhaps keep track of the last non-null string:

#include <string.h>

int main(void)
{
   char str[] = "now # is the time for all # good men to come to the # aid of their country";
   char delims[] = "#";
   char *result = NULL, *last;
   result = strtok( str, delims );
   while ( result != NULL )
   {
      last = result;
      printf( "result is \"%s\"\n", result );
      result = strtok( NULL, delims );
   }
   printf("%s\n", last);
   return 0;
}

okay thanks Dave, that worked perfect.
that's one step of my program finished, i'll just keep using this thread for the rest of my problems with this program instead of making new ones.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.