Загрузка данных


// textBox1 — ввод строки
// listBox1 — вывод результата
// button1 — точки и запятые
// button2 — количество слов
// button3 — частый символ
// button4 — цифры
// button5 — длины слов

// 1. Количество точек и запятых
private void button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();

    string s = textBox1.Text;

    int dots = 0;
    int commas = 0;

    for (int i = 0; i < s.Length; i++)
    {
        if (s[i] == '.')
            dots++;

        if (s[i] == ',')
            commas++;
    }

    listBox1.Items.Add("Точек: " + dots);
    listBox1.Items.Add("Запятых: " + commas);
}

// 2. Количество слов
private void button2_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();

    string s = textBox1.Text;

    string[] words = s.Split(' ');

    int count = 0;

    for (int i = 0; i < words.Length; i++)
    {
        if (words[i] != "")
            count++;
    }

    listBox1.Items.Add("Количество слов: " + count);
}

// 3. Самый часто встречающийся символ
private void button3_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();

    string s = textBox1.Text;

    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];
        }
    }

    listBox1.Items.Add("Символ: " + maxChar);
    listBox1.Items.Add("Количество: " + maxCount);
}

// 4. Вывод цифр из строки
private void button4_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();

    string s = textBox1.Text;

    string result = "";

    for (int i = 0; i < s.Length; i++)
    {
        if (Char.IsDigit(s[i]))
            result += s[i] + " ";
    }

    listBox1.Items.Add(result);
}

// 5. Строка из длин слов
private void button5_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();

    string s = textBox1.Text;

    string[] words = s.Split(' ');

    string result = "";

    for (int i = 0; i < words.Length; i++)
    {
        if (words[i] != "")
            result += words[i].Length + " ";
    }

    listBox1.Items.Add(result);
}