extract words from string

I try to read words from text file and store words as strings.
I use getline(fin, *word, ' '). But when it met '\n' or other punctuation characters, it didn't cut strings. Therefore, I write a function to cut strings, and I get stuck here.
1
2
3
4
5
6
7
8
9
10
11
12
13
string word(string *str)
{
   string word;
   for(unsigned int i = 0; str[i] != '\0'; i++)
   {
      if(isspace(str[i]) || ispunct(str[i]))
      {
         //how do I cut strings into words when it ends with whitespace or tab or punctuation character?
      }
}

return word;
}
Have a look a the following, it may help you:
http://www.cplusplus.com/reference/clibrary/cstring/strtok/
It requires const char * delimiters while delimiters change.
I don't think it's a good idea to include whole things: \t,\n,{}:"?><_+|.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string word(string *str)
{
   char text[str.length()];
   char *x;
   string word;

   for(unsigned int i = 0; str[i] != '\0'; i++)
   {
      if(isspace(str[i]) || ispunct(str[i]))
      {
            x = strtok(text, str[i]);
            word = string(x);
      }
}

return word;
}


It displayed error when I compiled it: could not convert const *char to string (if I don't remember wrong).
Last edited on
Topic archived. No new replies allowed.