C# Count Words In Text File

How To Count The Number Of Words In A .txt File Using C#

Count Words In Text File Using C#

In This C# Tutorial  We Will See How To Get The Number Of Words Inside A Text File 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;
using System.IO;

namespace Csharp_Tutorials
{
    public partial class Text_File_Count_Words : Form
    {
        public Text_File_Count_Words()
        {
            InitializeComponent();
        }

        private void buttonCount_Click(object sender, EventArgs e)
        {

            string filePath = textBox1.Text;
            string[] lines = File.ReadAllLines(filePath);

            int count = 0;

            char[] separator = { ' ' };

            for(int i = 0; i < lines.Length - 1; i++)
            {
                count += lines[i].ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries).Length;
            }

            label1.Text = count.ToString();

        }
    }
}




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

get the number of words in a txt file using c#







Share this

Related Posts

Previous
Next Post »