quinta-feira, 3 de junho de 2021

C# - pesquisando ocorrências de caracteres em frase


Já sabemos que podemos usar o método Count()
para contar elementos em listas e arrays,
então logicamente fica fácil enumerar elementos
dentro de uma frase completa mostrando suas ocorrências,
afinal uma string é um array de caracteres.
Neste exemplo estamos usando o método Count()
para retornar o número de elementos em uma sequência
de caracteres, digitada numa entrada de dados
com um textBox do C#.
Desculpem-nos pelas muitas linhas utilizadas
no programa, isto ocorreu porque preferimos
utilizar um Form no lugar do console do Windows.
Veja o vídeo do programa funcionando:





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Textbox_to_Array {
    public partial class Form1 : Form {
        int a = 0;
        char c;
        string str_1;
        /*============================================================*/
        void Informe ( PaintEventArgs e ) {
            Graphics dc = e.Graphics;
            Font myFont = new System.Drawing.Font ( "Helvetica"11FontStyle.Italic );              
            Brush myBrush_1 = new SolidBrush ( System.Drawing.Color.Red );
            Brush myBrush_2 = new SolidBrush ( System.Drawing.Color.Blue );
            Brush myBrush_3 = new SolidBrush ( System.Drawing.Color.Black );
            dc.DrawString ( "Por: "myFontmyBrush_1260250 );
            dc.DrawString ( "Samuel Lima "myFontmyBrush_2290250 );
            dc.DrawString ( "sa_sp10@hotmail.com "myFontmyBrush_3260265 );
        }
        /*============================================================*/
        void Label_1 ( int Xint Yint Wint Hstring st ) {
            label1.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            9.75FSystem.Drawing.FontStyle.Italic
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            label1.ForeColor = System.Drawing.Color.Blue;
            label1.Location = new System.Drawing.Point ( XY );
            label1.Name = "label1";
            label1.Size = new System.Drawing.Size ( WH );
            label1.TabIndex = 2;
            label1.Text = st;
        }
        /*============================================================*/
        void TextBox_1 ( int Xint Yint Wint H ) {
            textBox1.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            8FSystem.Drawing.FontStyle.Italic
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            textBox1.ForeColor = System.Drawing.Color.Black;
            textBox1.Location = new System.Drawing.Point ( XY );
            textBox1.Name = "textBox1";
            textBox1.Size = new System.Drawing.Size ( WH );
            textBox1.TabIndex = 3;
        }
        /*============================================================*/
        void TextBox_2 ( int Xint Yint Wint H ) {
            textBox2.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            8FSystem.Drawing.FontStyle.Italic,
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            textBox2.ForeColor = System.Drawing.Color.Black;
            textBox2.Location = new System.Drawing.Point ( XY );
            textBox2.Name = "textBox2";
            textBox2.Size = new System.Drawing.Size ( WH );
            textBox2.TabIndex = 4;
        }
        /*============================================================*/
        void Label_2 ( int Xint Yint Wint Hstring st ) {
            label2.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            9.75FSystem.Drawing.FontStyle.Italic
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            label2.ForeColor = System.Drawing.Color.Blue;
            label2.Location = new System.Drawing.Point ( XY );
            label2.Name = "label2";
            label2.Size = new System.Drawing.Size ( WH );
            label2.TabIndex = 5;
            label2.Text = st;
        }
        /*============================================================*/
        void Label_3 ( int Xint Yint Wint Hstring st ) {
            this.label3.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            9.75FSystem.Drawing.FontStyle.Italic
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            this.label3.ForeColor = System.Drawing.Color.Red;
            this.label3.Location = new System.Drawing.Point ( XY );
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size ( WH );
            label3.TabIndex = 6;
            label3.Text = st;
        }
        /*============================================================*/
        void Label_4 ( int Xint Yint Wint Hstring st ) {
            this.label4.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            9.75FSystem.Drawing.FontStyle.Italic,
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            this.label4.ForeColor = System.Drawing.Color.Blue;
            this.label4.Location = new System.Drawing.Point ( XY );
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size ( WH );
            label4.TabIndex = 7;
            label4.Text = st;
        }
        /*============================================================*/
        void Label_5 ( int Xint Yint Wint Hstring st ) {
            this.label5.Font = new System.Drawing.Font ( "Microsoft Sans Serif",
            9.75FSystem.Drawing.FontStyle.Italic,
            System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
            this.label5.ForeColor = System.Drawing.Color.Red;
            this.label5.Location = new System.Drawing.Point ( XY );
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size ( WH );
            label5.TabIndex = 8;
            label5.Text += st;
        }
        /*============================================================*/
        public void Button_1 ( int Xint Yint Wint Hstring st ) {
            textBox1.Focus ( );
            button1.Location = new Point ( XY );
            this.button1.Size = new System.Drawing.Size ( WH );
            button1.Font = new Font ( "Times New Roman"10F,
             System.Drawing.FontStyle.ItalicSystem.Drawing.GraphicsUnit.Point0 );
            button1.BackColor = Color.Pink;
            button1.ForeColor = Color.FromArgb ( 000 );
            this.button1.Text = st;
        }
        /*============================================================*/
        public void Button_2 ( int Xint Yint Wint Hstring st ) {
            button2.Location = new Point ( XY );
            this.button2.Size = new System.Drawing.Size ( WH );
            button2.Font = new Font ( "Times New Roman"13F,
             System.Drawing.FontStyle.ItalicSystem.Drawing.GraphicsUnit.Point0 );
            button2.BackColor = Color.LightGreen;
            button2.ForeColor = Color.FromArgb ( 000 );
            this.button2.Text = st;
        }
        /*============================================================*/
        public void Button_3 ( int Xint Yint Wint Hstring st ) {
            button3.Location = new Point ( XY );
            this.button3.Size = new System.Drawing.Size ( WH );
            button3.Font = new Font ( "Times New Roman"10F,
            System.Drawing.FontStyle.ItalicSystem.Drawing.GraphicsUnit.Point0 );
            button3.BackColor = Color.Yellow;
            button3.ForeColor = Color.FromArgb ( 000 );
            this.button3.Text = st;
        }
        /*============================================================*/
        public void Button_4 ( int Xint Yint Wint Hstring st ) {
            button4.Location = new Point ( XY );
            this.button4.Size = new System.Drawing.Size ( WH );
            button4.Font = new Font ( "Times New Roman"10F,
            System.Drawing.FontStyle.ItalicSystem.Drawing.GraphicsUnit.Point0 );
            button4.BackColor = Color.LightCyan;
            button4.ForeColor = Color.FromArgb ( 000 );
            this.button4.Text = st;
        }
        /*============================================================*/
        private void Paint_Imagem ( PaintEventArgs e ) {
            Image newImage = Image.FromFile ( "Moldura_2.bmp" );
            Point ulCorner = new Point ( 00 );
            e.Graphics.DrawImage ( newImageulCorner );
        }
        /*============================================================*/
        protected override void OnPaint ( PaintEventArgs e ) {
            Paint_Imagem ( e );
            this.Size = new System.Drawing.Size ( 615337 );
            this.Text = "C# - PESQUISANDO OCORRÊNCIAS DE CARACTERES EM FRASE";
            this.BackColor = Color.White;
            Graphics dc = e.Graphics;
            Font myFont = new System.Drawing.Font ( "Consolas"12,
                FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( Color.Red );
            dc.DrawString ( "C# - PESQUISANDO OCORRÊNCIAS DE CARACTERES EM FRASE",
                myFontmyBrush_17015 );
            Informe ( e );
        }
        /*============================================================*/
        public Form1 ( ) {
            InitializeComponent ( );
        }
        /*============================================================*/
        private void Button1_Click ( object senderEventArgs e ) {
            a++;
            //str_1 recebe a frase digitada no textBox1
            str_1 = textBox1.Text;

            if ( a == 1 ) {
                Console.Beep ( );
                label1.Visible = false;
                textBox1.Clear ( );
                textBox1.Visible = false;
                button1.Visible = false;

                Label_2 ( 100808080"Frase digitada" );
                Label_3 ( 1001008080"" + str_1 );
                Label_4 ( 1001308080"Caractere para pesquisar: " );

                textBox2.Visible = true;
                button3.Visible = true;
                TextBox_2 ( 2701253010 );
                Button_3 ( 3201253522"Ok" );
                _ = textBox2.Focus ( );
            }
        }
        /*============================================================*/
        private void Form1_Shown ( object senderEventArgs e ) {
            textBox2.Visible = false;
            button1.Enabled = true;
            button3.Visible = false;
            button4.Visible = false;

            label1.Visible = true;
            Label_1 ( 1005020020"Digite uma frase: " );
            TextBox_1 ( 2104513010 );
            Button_1 ( 360453522"Ok" );
            Button_2 ( 2002605025"Sair" );
        }
        /*============================================================*/
        private void button3_Click ( object senderEventArgs e ) {
            //Entra com um caractere no textBox2  
            c = char.Parse ( textBox2.Text );

            //Conta o total de ocorrências do caractere na frase
            int ocorre = str_1.Count ( t => t == c );

            textBox2.Clear ( );
            _ = textBox2.Focus ( );

            Label_5 ( 10017020020"" );
            label5.Text = "";
            label5.Visible = true;
            label5.Text += "Encontrado ";
            label5.Text += ocorre;
            label5.Text += " ocorrênciasc de ";
            label5.Text += c;

            button4.Visible = true;
            Button_4 ( 20022012025"Nova operação" );         
        }
        /*============================================================*/
        private void button4_Click ( object senderEventArgs e ) {
            button3.Visible = false;
            button4.Visible = false;
            textBox2.Visible = false;

            textBox1.Visible = true;
            button1.Visible = true;

            label2.Text = "";
            label3.Text = "";
            label4.Text = "";
            label5.Text = "";

            label1.Visible = true;
            Label_1 ( 1005020020"Digite uma frase: " );

            a = 0;
            c = ' ';
            str_1 = " ";
            textBox1.Clear ( );
            _ = textBox1.Focus ( );
            textBox2.Clear ( );
        }
        /*============================================================*/
        private void button2_Click ( object senderEventArgs e ) {
            Application.Exit ( );
        }
        /*============================================================*/
    }
}

Nenhum comentário:

Postar um comentário