D
D
DL11112020-11-26 23:19:58
C++ / C#
DL1111, 2020-11-26 23:19:58

How can I change the Windows forms code so that it counts the number of letters in each word?

private void button1_Click(object sender, EventArgs e)
        {
            int index = listBox.SelectedIndex;

            string rts = (string)listBox1.Items[index];

            int len = str.Length;

            int count = 0;

            int i = 0;

            while(i < len)
            {
                if (str[i] == " ")
                    count++;
                i++;
            }
            Label1.text = "Количество пробелов = " +
                count.ToString();
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BasiC2k, 2020-11-27
@BasiC2k

I understand that you need to count certain letters.
The correct option is to use RegEx

C
cicatrix, 2020-11-27
@cicatrix

Well, if you do it on the forehead as it is written in the condition, then:

string input  = "Раз два три четыре пять, я иду искать";
string[] words = input.Split(new char[] { ' ', ',', '.', '-', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
foreach(var word in words) { Console.WriteLine($"{word}: длина {word.Length}"); }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question