& I wanna create text file containing one name on each line. Compute the number of times any name occurs. Output one line for each name in file and on each line print the number of occurrences followed by name.
and display the result in MessegeBox.Show
1
Expert's answer
2012-07-27T07:26:57-0400
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { & public Form1() & { InitializeComponent(); & }
& private void button1_Click(object sender, EventArgs e) & { string[] names = richTextBox1.Text.Split('\n'); List<string> newNames = new List<string>(); List<int> newNamesIndex = new List<int>(); & for (int i = 0; i < names.Length; i++) { int k = 0; for (int j = 0; j < newNames.Count; j++) { & if (newNames[j] != names[i]) & { k++; & } & else & { newNamesIndex[j]++; & } } if (k == newNames.Count) { & newNames.Add(names[i]); & newNamesIndex.Add(1); } }
richTextBox1.Clear(); for (int i = 0; i < newNames.Count; i++) { richTextBox1.Text += newNames[i] + " " + newNamesIndex[i] + "\n"; } & } } }
Comments
Leave a comment