Click here to Skip to main content
15,041,788 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

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
   
v2
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


       }

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

MEssageBox.Show(Convert.ToString(path.Split("CIRCLE").Length));

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