I would like to read a txt file and then extract the words in an array.
e.g.
(A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.)
And I would like to store only the word and ignore all the punctuation and spaces.
This is the function I wrote when could only deal with the single space. How could I amend it or simply write a new one to deal with the above purpose? Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int substring(string str,string li[]){
//convert to lower case
for (int i=0;i<str.length();i++)
str[i]=tolower(str[i]);
//divide the sentense into substring
int i(0); // numebr of word
int strpos(0);// start position
int endpos= str.find(' '); //the position of ' '
for(i;endpos>0;i++){
li[i]=str.substr(strpos,endpos-strpos);
strpos=endpos+1;
endpos=str.find(' ',strpos);
if(endpos<0){i++;li[i]=str.substr(strpos);}
}
return i;
}
Are you actually interested in the words, or only in the letters? Your above code looks a bit over-complex if you don't need to split the text into words.