C
C#13mo ago
Spekulant

❔ ✅ whitespaces are doing me dirty

public void Execute() {
StringBuilder word = new StringBuilder();
int wordCount = 0;
bool multipleNewLines = false;

int charValue;
while ((charValue = _reader.Read()) != -1) {
char character = (char)charValue;
if (!Char.IsWhiteSpace(character)) {
word.Append(charValue);
multipleNewLines = false;
} else {
if (word.Length > 0) {
wordCount++;
word.Clear();
}
if (character == '\n') {
multipleNewLines = true;}
if (character == '\n' && multipleNewLines) {
if (wordCount != 0) { _paragraphWordCount.Add(wordCount); }
wordCount = 0;
multipleNewLines = false;
}
}
}
if (word.Length > 0) {
wordCount++;
_paragraphWordCount.Add(wordCount);
}
}
public void Execute() {
StringBuilder word = new StringBuilder();
int wordCount = 0;
bool multipleNewLines = false;

int charValue;
while ((charValue = _reader.Read()) != -1) {
char character = (char)charValue;
if (!Char.IsWhiteSpace(character)) {
word.Append(charValue);
multipleNewLines = false;
} else {
if (word.Length > 0) {
wordCount++;
word.Clear();
}
if (character == '\n') {
multipleNewLines = true;}
if (character == '\n' && multipleNewLines) {
if (wordCount != 0) { _paragraphWordCount.Add(wordCount); }
wordCount = 0;
multipleNewLines = false;
}
}
}
if (word.Length > 0) {
wordCount++;
_paragraphWordCount.Add(wordCount);
}
}
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
nohopestage
nohopestage13mo ago
Is there a specific reason you're not using regex?
dan in a can
dan in a can13mo ago
you can try Environment.Newline instead of \n
Servator
Servator13mo ago
Just count spaces and Environment.Newline and return +1 of it seems like you are overcomplicating the problem
Spekulant
SpekulantOP13mo ago
i have yo use char by char read because otherwise the space and time will be too much
nohopestage
nohopestage13mo ago
What do you mean by "the space and time will be too much"?
Bailey
Bailey13mo ago
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
No description
Bailey
Bailey13mo ago
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()}");
Spekulant
SpekulantOP13mo ago
!close
Accord
Accord13mo ago
Closed!
MODiX
MODiX13mo ago
That command had an error
UnknownCommand: Unknown command.
Remove your reaction to delete this message
Accord
Accord13mo ago
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.
Want results from more Discord servers?
Add your server