Before passing each char into FreqTbl.buildTable you have option to process your input:
string strInput = textBox1.Text.TrimEnd('\r', '\n' );
Or
string strInput = textBox1.Text.TrimEnd(Environment.NewLine.ToCharArray());
Question
I'm creating a program in C# that goes through a string and calculate the frequency of characters in it.
Basically I have 2 class
class Node
{
private char character;
private float frequency;
public void increment();//increments frequency
}
class FreqTbl
{
private List<Node> nodes;
public void buildTable(char Symbol)
{
bool found = false;
foreach (var node in nodes.Where(n => n.getSymbol() == Symbol))
{
node.increment();
found = true;
}
if(found == false)
{
newNode(Symbol);
}
}
}
Through a text box GUI, The program passes a char into FreqTbl.buildTable , which search through the nodes field. While all other characters are processed no problem, line breaks appears twice in the output
Say if the text box have the input
linked list
red black tree
The resultant output would be
l:3
i:2
n:1
k:2
e:4
d:2
:3
s:1
t:2
\n
:1
\n
:1
r:2
b:1
a:1
c:1
\n represents the line break. Anyway to fix this so that only 1 line breaks appear in the output?
Solution
Before passing each char into FreqTbl.buildTable you have option to process your input:
string strInput = textBox1.Text.TrimEnd('\r', '\n' );
Or
string strInput = textBox1.Text.TrimEnd(Environment.NewLine.ToCharArray());