Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello,
how to write a program to count words ,character and lines in a text file in c#.......


Thanks & Regards
Srishti
Posted
Updated 4-Jul-21 4:07am

C#
class Demo
   {
       public static void Main()
       {
           string path = File.ReadAllText("D:\\jittu.txt");

           Console.WriteLine(path); // it ll display ur file on console

           Console.Write(path.Length); // show character
           Console.WriteLine();

           Console.WriteLine(path.Split('\r').Length);  // total line

           Console.WriteLine(path.Split(' ').Length); // total word


       }

   }
 
Share this answer
 
Comments
Member 14039341 1-Jan-19 12:15pm    
How do I search for strings?

MEssageBox.Show(Convert.ToString(path.Split("CIRCLE").Length));
You can try this also
C#
int  WordsCount = CharCount = LinesCount = 0;
          string FilePath = "your file path here";
            string FileText = new System.IO.StreamReader(FilePath).ReadToEnd().Replace("\r\n", "\r");
            CharCount = FileText.Length;            //total char 
            LinesCount = FileText.Split('\r').Length;       //total lines
            WordsCount = FileText.Split(' ').Length;        // total Words
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900