I'm trying to write a simple program which takes a textfile, sets all characters to lowercase and removes all punctuation. My problem is that when there is a carriage return (I believe that's what it is called) and a new line, the space is removed.
i.e.
This is a
test sentence
Becomes
This is atestsentence
The last word of the first line and the first word of the next line are joined.
This is my code:
public static void ParseDocument(String FilePath, String title)
{
StreamReader reader = new StreamReader(FilePath);
StreamWriter writer = new StreamWriter("C:/Users/Matt/Documents/"+title+".txt");
int i;
char previous=' ';
while ((i = reader.Read())>-1)
{
char c = Convert.ToChar(i);
if (Char.IsLetter(c) | ((c==' ') & reader.Peek()!=' ') | ((c==' ') & (previous!=' ')))
{
c = Char.ToLower(c);
writer.Write(c);
}
previous = c;
}
reader.Close();
writer.Close();
}
It's a simple problem, but I can't think of a way of checking for a new line to insert a space. Any help is greatly appreciated.