❔ ✅ whitespaces are doing me dirty
Im reading char by char and i just want to count how many words are in paragraphs but it doesnt count correctly there is a problem with /n
11 Replies
Is there a specific reason you're not using regex?
you can try
Environment.Newline
instead of \n
Just count spaces and Environment.Newline and return +1 of it
seems like you are overcomplicating the problem
i have yo use char by char read because otherwise the space and time will be too much
What do you mean by "the space and time will be too much"?
Hi,
I put a printscreen in here.
How to find the cause of the mismatch.
just put in breakpoints and see what happens.
If you look at: if (character == '\n') ==> when this one is true the next if is also triggered. My advice is try to solve this. In the next message I put in another way like yourse
other way / different written:
<
Also I created 1 just another way.
string testpar = "his stuf\nnew is here.\n\nNew paragraph is here";
int lineCount = 0;
int wordcount = 0;
var convertedToChararray = testpar.ToCharArray();
var word = string.Empty;
foreach (char c in convertedToChararray)
{
if (!char.IsWhiteSpace(c))
{
word += c.ToString();
lineCount = 0;
}
if (char.IsWhiteSpace(c))
{
if (word.Length > 0)
wordcount++;
word = string.Empty;
if (c == '\n')
lineCount++;
}
if (c == '\n' && lineCount > 1)
{
Console.WriteLine($"WordCount = {wordcount}");
wordcount = 0;
lineCount = 0;
}
}
if (true)
{
if (!string.IsNullOrEmpty(word))
{
wordcount++;
}
}
Console.WriteLine($"words: {wordcount.ToString()}");
!close
Closed!
That command had an error
UnknownCommand: Unknown command.
Remove your reaction to delete this message
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.