Word count not working when entering new line

269 views Asked by At

I've been looking at other stack overflow articles regarding similar issues when it comes to word count in C#, but none have helped me when it comes to the pickle I've encountered.

I have a textbox that inputs text from a text file. The text is split into three lines by me pressing the enter button to create a new line in a text file. The text reads:

It's a test to see "if" the
application_for Top Image Systems
actually work. Hopefully it does work.

Now as you can see there should be 17 words, however my word count only says 15. I have realized after a bit of trial and error that the issue must be the fact it's in a new line. Every time it goes to a new line, it thinks the last word of the previous line and the first word of the new line are together as a word (or that's what I think the program is thinking).

My question is with the code I have below, how can I get to recognize that if there is a new line, that it should split the words like a space?

Below is my code:

string nl = System.Environment.NewLine;

//Missing Code to read text file which I don't need to include in this example

do
{
    textLine = textLine + txtReader.ReadLine();
}

//Read line until there is no more characters
while (txtReader.Peek() != -1);

//seperate certain characters in order to find words
char[] seperator = (" " + nl).ToCharArray();

//number of words
int numberOfWords = textLine.Split(seperator, StringSplitOptions.RemoveEmptyEntries).Length;
2

There are 2 answers

4
h3n On BEST ANSWER

txtReader.ReadLine(); strips your newline away. From the msdn:

The string that is returned does not contain the terminating carriage return or line feed.

so you have to add it manually (or just add a space)

textLine = textLine + txtReader.ReadLine() + " ";

consider using the StringBuilder class for repeated concatination of strings.

Edit:

To get the character count as if the spaces were never added, do:

int charCount = textLine.Length - lineCount;

where lineCount an integer that you increment every time you add a space in your do-while loop:

int lineCount = 0;

do
{
    textLine = textLine + txtReader.ReadLine();
    lineCount++;
}
3
AMTickityB On

I'm a bit of a beginner myself, sorry if this is not a great answer, but I've just done a bunch of text stuff in c# and I'd probably approach by replacing the line breaks which will show up as "\n" or "\r" in your original string with a space, " " - something like:

nl = nl.Replace("\r", " ");