C# Count Character Occurrence

How To Count Specific Character Occurrence In A String Using C# 

C# Number Of Char Occurrences

In This C# Tutorial  We Will See How To Count The Number Of Selected Char Occurrences In A TextBox Text Using CSharp Programming Language And Visual Studio Editor.


Project Source Code:

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 WindowsFormsApplication1
{
    public partial class CharOccurrence : Form
    {
        public CharOccurrence()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string txt = textBox1.Text;

            int occurrence = 0;

            for(int i = 0; i < txt.Length; i++)
            {
                if (txt[i] == textBox2.Text.ToCharArray()[0])
                {
                    occurrence++;
                }

            }
            MessageBox.Show(occurrence.ToString());

        }
    }
}



///////////////OUTPUT:

specific char number in a string using c#







Share this

Related Posts

Previous
Next Post »