Reputation: 3
I am trying to print a list of occurrences of each letter in thee text inputed by the user but the textbox only shows the last letter.
private void button1_Click(object sender, EventArgs e)
{
//disable the text box so text cannot be entered until reset is pressed
textBox3.Enabled = false;
//make a list containing the alphabets
List<string> Alphabets = new List<string>();
Alphabets.Add("A");
Alphabets.Add("B");
Alphabets.Add("C");
Alphabets.Add("D");
Alphabets.Add("E");
Alphabets.Add("F");
Alphabets.Add("G");
Alphabets.Add("H");
Alphabets.Add("I");
Alphabets.Add("J");
Alphabets.Add("K");
Alphabets.Add("L");
Alphabets.Add("M");
Alphabets.Add("N");
Alphabets.Add("O");
Alphabets.Add("P");
Alphabets.Add("Q");
Alphabets.Add("R");
Alphabets.Add("S");
Alphabets.Add("T");
Alphabets.Add("W");
Alphabets.Add("X");
Alphabets.Add("Y");
Alphabets.Add("Z");
//make a while loop to cycle through alphabets loop
int i = 0;
while (i < Alphabets.Count)
{
//assign value in the list Alphabets
string SearchFor = Alphabets[i];
//access textbox and make it to upper small small and captital are the same
string SearchIn = textBox3.Text.ToUpper();
//list to count the number of instances used for each letter
List<int> FoundCount = Search(SearchFor, SearchIn);
//if statement so only letters that occor more than 0 times are displayed
if (FoundCount.Count > 0)
{
//string to display the number of occorances
//convert to toString so it can be displayed in the TextBox
string Counts = Alphabets[i] + ": " + FoundCount.Count.ToString();
//create a message box to display the output
//MessageBox.Show(Counts);
textBox4.Text = String.Join(Environment.NewLine, Counts);
}
//adds 1 each time as long as i <alphabet.count
i++;
}
}
//List takes 2 arguements (SearchFor, SearchIn) Results can be passed to FoundCounts and number of occrances can be calculted
List<int> Search(string Target, string Subject)
{
//List to count the number of occarances for each letter
List<int> Results = new List<int>();
//for loop for comparison (counting occarances) to compare each letter in textbox to each letter in alphabets
for (int Index = 0; Index < (Subject.Length - Target.Length) + 1; Index++)
{
//if to calculate the number of times a letter is used.
if (Subject.Substring(Index, Target.Length) == Target)
{
//adds the number in index to the list Result
Results.Add(Index);
}
}
return Results;
}
i made multiline as true but that didnt work
Upvotes: 0
Views: 81
Reputation: 117144
This isn't a direct answer to the question, but this might be valuable for the OP.
This code does everything that the original code did (and also fixes the issue raised in the question):
private void button1_Click(object sender, EventArgs e)
{
var SearchIn = textBox3.Text.ToUpper();
var query =
from c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let count = SearchIn.Count(x => x == c)
select String.Format("{0}: {1}", c, count);
textBox4.Text = String.Join(Environment.NewLine, query);
}
There you go, a version that uses lists. :-)
private void button1_Click(object sender, EventArgs e)
{
var SearchIn = textBox3.Text.ToUpper();
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToList();
var query =
from c in alphabet
let matches = SearchIn.Where((x, n) => x == c).ToList()
select String.Format("{0}: {1}", c, matches.Count());
textBox4.Text = String.Join(Environment.NewLine, query);
}
Upvotes: 1
Reputation: 15158
I'm pretty sure the issue with your code is here:
textBox4.Text = String.Join(Environment.NewLine, Counts);
You're overwriting the text every time, instead you should do:
textBox4.Text += String.Join(Environment.NewLine, Counts);
Upvotes: 2