I have a text file containing just lowercase letters and no punctuation except for spaces. I would like to know the best way of reading the file char by char, in a way that if the next char is a space, it signifies the end of one word and the start of a new word. i.e. as each character is read it is added to a string, if the next char is space, then the word is passed to another method and reset until the reader reaches the end of the file.
I'm trying to do this with a StringReader, something like this:
public String GetNextWord(StringReader reader)
{
String word = "";
char c;
do
{
c = Convert.ToChar(reader.Read());
word += c;
} while (c != ' ');
return word;
}
and put the GetNextWord method in a while loop till the end of the file. Does this approach make sense or are there better ways of achieving this?
StringBUilder
if you want to build the string while reading the file. - Brian Rasmussen