Enter a string of characters to analyze the characters in the string and how many times each character appears

Source: Internet
Author: User

I made a question for GF and did it myself.
Question:
Enter a string of characters to analyze which characters appear in the string and how many times each character appears.
Answer:
Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;

Namespace yanysQustion
{
/// <Summary>
/// Summary of Form1.
/// </Summary>
Public class Form1: System. Windows. Forms. Form
{
Private System. Windows. Forms. Label label1;
Private System. Windows. Forms. Label label2;
Private System. Windows. Forms. Button button1;
Private System. Windows. Forms. Button button2;
Private System. Windows. Forms. Button button3;
Private System. Windows. Forms. Label label4;
Private System. Windows. Forms. ColorDialog colorDialog1;
Private System. Windows. Forms. TextBox t_scr;
Public System. Windows. Forms. TextBox t_res;
Private System. Windows. Forms. Button button4;
Public System. Windows. Forms. Label msg;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;

Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();

//
// TODO: add Any constructor code after InitializeComponent calls
//
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. t_scr = new System. Windows. Forms. TextBox ();
This. label1 = new System. Windows. Forms. Label ();
This. label2 = new System. Windows. Forms. Label ();
This. button1 = new System. Windows. Forms. Button ();
This. t_res = new System. Windows. Forms. TextBox ();
This. msg = new System. Windows. Forms. Label ();
This. button2 = new System. Windows. Forms. Button ();
This. button3 = new System. Windows. Forms. Button ();
This. label4 = new System. Windows. Forms. Label ();
This. colorDialog1 = new System. Windows. Forms. ColorDialog ();
This. button4 = new System. Windows. Forms. Button ();
This. SuspendLayout ();
//
// T_scr
//
This. t_scr.BorderStyle = System. Windows. Forms. BorderStyle. FixedSingle;
This. t_scr.Location = new System. Drawing. Point (0, 24 );
This. t_scr.Multiline = true;
This. t_scr.Name = "t_scr ";
This. t_scr.Size = new System. Drawing. Size (480, 72 );
This. t_scr.TabIndex = 0;
This. t_scr.Text = "";
//
// Label1
//
This. label1.Location = new System. Drawing. Point (8, 8 );
This. label1.Name = "label1 ";
This. label1.Size = new System. Drawing. Size (128, 16 );
This. label1.TabIndex = 1;
This. label1.Text = "enter a string of characters :";
//
// Label2
//
This. label2.Location = new System. Drawing. Point (0,104 );
This. label2.Name = "label2 ";
This. label2.Size = new System. Drawing. Size (100, 16 );
This. label2.TabIndex = 2;
This. label2.Text = "Character Parsing :";
//
// Button1
//
This. button1.FlatStyle = System. Windows. Forms. FlatStyle. Popup;
This. button1.Location = new System. Drawing. Point (144,128 );
This. button1.Name = "button1 ";
This. button1.TabIndex = 3;
This. button1.Text = "analysis ";
This. button1.Click + = new System. EventHandler (this. button#click );
//
// T_res
//
This. t_res.BorderStyle = System. Windows. Forms. BorderStyle. FixedSingle;
This. t_res.Location = new System. Drawing. Point (0,128 );
This. t_res.Multiline = true;
This. t_res.Name = "t_res ";
This. t_res.ScrollBars = System. Windows. Forms. ScrollBars. Vertical;
This. t_res.Size = new System. Drawing. Size (136,224 );
This. t_res.TabIndex = 0;
This. t_res.Text = "";
//
// Msg
//
This. msg. BorderStyle = System. Windows. Forms. BorderStyle. FixedSingle;
This. msg. ForeColor = System. Drawing. Color. Red;
This. msg. Location = new System. Drawing. Point (224,128 );
This. msg. Name = "msg ";
This. msg. Size = new System. Drawing. Size (256,224 );
This. msg. TabIndex = 4;
//
// Button2
//
This. button2.FlatStyle = System. Windows. Forms. FlatStyle. Popup;
This. button2.Location = new System. Drawing. Point (144,208 );
This. button2.Name = "button2 ";
This. button2.TabIndex = 3;
This. button2.Text = "background color ";
This. button2.Click + = new System. EventHandler (this. button2_Click );
//
// Button3
//
This. button3.FlatStyle = System. Windows. Forms. FlatStyle. Popup;
This. button3.Location = new System. Drawing. Point (144,248 );
This. button3.Name = "button3 ";
This. button3.TabIndex = 3;
This. button3.Text = "font color ";
This. button3.Click + = new System. EventHandler (this. button3_Click );
//
// Label4
//
This. label4.Location = new System. Drawing. Point (224,112 );
This. label4.Name = "label4 ";
This. label4.Size = new System. Drawing. Size (100, 16 );
This. label4.TabIndex = 5;
This. label4.Text = "analysis :";
//
// Button4
//
This. button4.FlatStyle = System. Windows. Forms. FlatStyle. Popup;
This. button4.Location = new System. Drawing. Point (144,168 );
This. button4.Name = "button4 ";
This. button4.TabIndex = 3;
This. button4.Text = "empty ";
This. button4.Click + = new System. EventHandler (this. button4_Click );
//
// Form1
//
This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
This. ClientSize = new System. Drawing. Size (480,357 );
This. Controls. Add (this. label4 );
This. Controls. Add (this. msg );
This. Controls. Add (this. button1 );
This. Controls. Add (this. label2 );
This. Controls. Add (this. label1 );
This. Controls. Add (this. t_scr );
This. Controls. Add (this. t_res );
This. Controls. Add (this. button2 );
This. Controls. Add (this. button3 );
This. Controls. Add (this. button4 );
This. Name = "Form1 ";
This. Text = "y ";
This. ResumeLayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. Run (new Form1 ());
}
// Background color
Private void button2_Click (object sender, System. EventArgs e)
{
// Enable the color control and fine-tune the color.
ColorDialog1.AllowFullOpen = true;
// Display the Help button
ColorDialog1.ShowHelp = true;
// Open the color selection box and wait for the user to select the color and then click "OK"
If (colorDialog1.ShowDialog () = DialogResult. OK)
T_res.BackColor = colorDialog1.Color;

}
// Font color
Private void button3_Click (object sender, System. EventArgs e)
{
// Do not allow users to fine-tune the color
ColorDialog1.AllowFullOpen = false;
// Display the Help button
ColorDialog1.ShowHelp = true;
// Open the color selection box and wait for the user to select the color and then click "OK"
If (colorDialog1.ShowDialog () = DialogResult. OK)
T_res.ForeColor = colorDialog1.Color;
}
// Resolution
Private void button#click (object sender, System. EventArgs e)
{
// The string cannot be blank.
If (t_scr.Text = "")
{
MessageBox. Show ("enter a string of characters! ");
Return;
}
TimeSpan t1 = new TimeSpan (DateTime. Now. Ticks );
String soruce = t_scr.Text;
Int strLen = soruce. Length;
Int realLen = 0;
String realStr = "";
For (int I = 0; I <strLen; I ++)
{
If (soruce. IndexOf (soruce [I], I) = soruce. LastIndexOf (soruce [I])
RealStr + = soruce [I];
}
RealLen = realStr. Length;
T_res.Text = string. Empty;
For (int I = 0; I <realLen; I ++)
T_res.AppendText (realStr [I] + "occurrences:" + (soruce. Split (realStr [I]). Length-1) + "\ r \ n ");
TimeSpan t2 = new TimeSpan (DateTime. Now. Ticks );
TimeSpan ts = t2.Subtract (t1). Duration ();
Msg. Text = "original String Length:" + strLen + "\ r \ n" +
"Number of non-repeated characters:" + realLen + "\ r \ n" +
"Running time:" + ts. Minutes + "Minute" + ts. Seconds + "second" + ts. Milliseconds + "millisecond ";

}
// Clear
Private void button4_Click (object sender, System. EventArgs e)
{
T_scr.Text = string. Empty;
T_res.Text = string. Empty;
Msg. Text = string. Empty;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.