using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("Мама мыла раму");
listBox1.Items.Add("Привет, мир!");
listBox1.Items.Add("Visual C# 2025");
listBox1.Items.Add("Томский политехнический университет");
}
// button1 — пример из методички
private void button1_Click(object sender, EventArgs e)
{
string s = listBox1.SelectedItem.ToString();
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == ' ')
count++;
}
label1.Text = "Количество пробелов = " + count;
}
// button2 — количество точек и запятых
private void button2_Click(object sender, EventArgs e)
{
string s = listBox1.SelectedItem.ToString();
int dots = 0;
int commas = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '.')
dots++;
if (s[i] == ',')
commas++;
}
label1.Text = "Точек = " + dots +
" Запятых = " + commas;
}
// button3 — количество слов
private void button3_Click(object sender, EventArgs e)
{
string s = listBox1.SelectedItem.ToString();
string[] words = s.Split(' ');
int count = 0;
for (int i = 0; i < words.Length; i++)
{
if (words[i] != "")
count++;
}
label1.Text = "Количество слов = " + count;
}
// button4 — самый часто встречающийся символ
private void button4_Click(object sender, EventArgs e)
{
string s = listBox1.SelectedItem.ToString();
char maxChar = ' ';
int maxCount = 0;
for (int i = 0; i < s.Length; i++)
{
int count = 0;
for (int j = 0; j < s.Length; j++)
{
if (s[i] == s[j])
count++;
}
if (count > maxCount)
{
maxCount = count;
maxChar = s[i];
}
}
label1.Text = "Символ = " + maxChar +
" Количество = " + maxCount;
}
// button5 — вывести цифры
private void button5_Click(object sender, EventArgs e)
{
string s = listBox1.SelectedItem.ToString();
string result = "";
for (int i = 0; i < s.Length; i++)
{
if (Char.IsDigit(s[i]))
result += s[i] + " ";
}
label1.Text = "Цифры: " + result;
}
// button6 — длины слов
private void button6_Click(object sender, EventArgs e)
{
string s = listBox1.SelectedItem.ToString();
string[] words = s.Split(' ');
string result = "";
for (int i = 0; i < words.Length; i++)
{
if (words[i] != "")
result += words[i].Length + " ";
}
label1.Text = "Длины слов: " + result;
}
}
}