C#——设计一个窗体程序,输入一行字符,检索是否存在重复的二字词汇(由两个字符组成的字符),输出重复的次数

设计界面:

编写代码:

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 aa

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            int n = 0;

            string[] words = new string[100];

            int[] number = new int[100];

            for (int i = 0; i < textBox1.Text.Length - 2; i++)

            {

                bool flag = false; //判断单词是否发生重复

                string sleft=textBox1 .Text .Substring (i,2);

                for (int j = i + 2; j < textBox1.Text.Length - 2;j++ )

                {

                    string sright = textBox1.Text.Substring(j,2);

                    if(sleft ==sright )

                    {

                        number[n]++;

                        if(Array .IndexOf (words ,sright )==-1)

                        {

                            flag = true;

                            words[n] = sright;

                        }

                    }

 

                }

                if (flag == true)//如果找到了重复的单词,单词数组中的索引值加1

                {

                    n++;

                }

            }

            label2.Text = string.Format("一共有{0}个重复的词汇",n);

            for (int z = 0; z < n;z++ )

            {

                label2.Text += string.Format("\n\n\n单词:“{0}”重复{1}次?", words[z], number[z] + 1);

            }

 

        }

    }

}

 

 

运行结果:

 

猜你喜欢

转载自blog.csdn.net/lmm0513/article/details/88773363