Site Loader
Auckland, New Zealand
Using the below given code snippet, we can open a text file and read the number of given character occurs Here is the algorithm Here is the code snippet
private static void ReadNoOfSimilarChar(string word)
        {
            StreamReader reader = File.OpenText(@"c:\data.txt");
            int letterCount = 0;
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();

                for (int i = 0; i < line.Length; i++)
                {
                    if (line.Substring(i, 1) == word)
                    {
                        letterCount++;
                    }
                }
            }

            Console.Write(letterCount);
            Console.Read();
        }
Thanks, Karthik KK

Post Author: Karthik kk

One Reply to “Count number of similar Characters of a text file using C#”

Leave a Reply

Your email address will not be published. Required fields are marked *