i'm trying to get the last word of the sentence using strtok.. i tried this :

while (tok != NULL)
  {
    tok = strtok (NULL, " ");
    if((tok != NULL) && (strtok(NULL, " ")==NULL))
            printf("%s", tok);
  }

kinda doesn't work... anyone knows why?
Yes, tok is a char * pointer..

>>anyone knows why?
Yes.

how about something like this:

char* lastWord = NULL;
while( tok != NULL )
{
   lastWord = tok;
   tok = strtok(NULL, " ");
}
printf("%s\n", lastWord);

thanks!
that looks like an obvious solution now...

you might have to add code to bypass all other white space (spaces and tabs) because I don't think strtok will do that. Example:

"No     is        the          time       "

kinda doesn't work... anyone knows why?

[edit] AD solved that problem already [/edit]

Anyway, if you're using C++, why not use std::strings, find() and substr()?
Demo:

#include <iostream>
#include <string>

int main ()
{
  std::string str = "This is a line of text";
  int lastspace= str.find_last_of(' ')  ;
  std::cout << str.substr(lastspace+1 ,str.size()-lastspace ) << "\n";
  return 0;
}

output: text

but that doesn't work when there is trailing white space. need to add a little code to trim trailing white space.

Yes you're right of course

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.