Store words from file in array

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;
}
The function isalpha() could be useful if you want to ignore everything which is not a letter.

http://www.cplusplus.com/reference/cctype/isalpha/

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.
Actually I want to split the text into words and store them into an array thanks for you information! It does help a lot.
I would do it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main()
{
  ifstream src("Data.txt");
  vector<string> Words;
  
  if (!src)
  {
    cout << "Error opening the file" << endl;
    exit(1);
  }  
  string word;
  while(src >> word)
  {
    // TO DO remove unwanted characters from word
    cout << word << endl;  //Just for testing
    Words.push_back(word);
  }

  system("pause");
}

Thomas1965 I had much the same idea. Plus one extra function to remove punctuation from each word.
Topic archived. No new replies allowed.