I have a project which is about linked lists in the order of: a Story is made of Paragraphs which is made of Sentences which is made of Words. We must treat Words specifically as character arrays, not strings. So I need to read in a story from a text file and make Words by finding the first whitespace/punctuation(everything before the whitespace up until the whitespace or punctuation is the Word), all the Words up to the punctuation are a Sentence (Sentence is a linked list of Words), Paragraph is all the Sentences up until an empty line, and a Story is all the Paragraphs. So I know doing this will let me take a line and put it into a character array:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
char charArray[25]; //we are allowed to assume a word won't be longer than 25 characters
int i = 0;
ifstream myFile(fileName.c_str());
if(myFile.is_open()){
while(!myfile.eof()){
myFile >> charArray[i];
i++;
}
myFile.close();
}
else{
cout << "Unable to open file";
}
|
But how can I break this up so that at a whitespace or punctuation it will take the charArray and package it into a Word? Like what is the syntax I can use to see if the current character it is reading in is ws or punctuation i.e
1 2 3 4 5
|
if(xxx == ' ' || xxx == '.' || etc){
{
take charArray and make it a Word
}
|
I suppose I could look at
charArray[i]
and if it is ws or punctuation then make the Word = charArray[i-1], but is there an if statement I could do that would prevent the ws or punctuation from being read into charArray in the first place? Because a problem I see with the charArray[i-1] method already is that "This is a story." would get put into the array as Thisisastory. and thus I'd be unable to break up at a space.
So to summarize:
I want to read in a text file character by character into a char array, which I can set to length 25 due to context of the project. I want to read in each character and at a whitespace or punctuation, I want to take everything already in charArray and feed that into a Word object constructor (the next Word gets linked to the previous Word, and at a punctuation all the Words linked together become one Sentence, and all the Sentences linked before an empty line become a Paragraph, etc). So how can I get charArray to be only characters in a word, then after the word being read-in ends, charArray resets to empty, and then is populated by the next word and so on.