Reading a file in C#

Below is a simple program in C# that reads a file and outputs the longest word in it. Enjoy!

using System;
using System.IO;
using System.Collections.Generic;
 
namespace <yourNS>
{
    class FilesNDirs
    {
 
        public static void Main(string[] argvs)
        {
            var path = @"C:\Users\deepesh\Desktop\Test\test.txt";
 
            var textFile = File.ReadAllText(path);
            var words = textFile.Split(' ');
            var wordlenpt = new string[100];
            var wordlenarr = new List<int>();
            foreach (var i in words)
            {
                if (i.Length > 0)
                {
                    wordlenarr.Add(i.Length);
                    wordlenpt[i.Length] = i;
                    Console.WriteLine(i);
                }
            }
            wordlenarr.Sort();
            foreach (var i in wordlenarr)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("\n\nLargest word in the file is >>" + 
wordlenpt[wordlenarr[wordlenarr.Count - 1]]);
        }
    }
}

Add a Comment

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