Create a program in C# to count the number of words stored in a text file.
public class CountWordsInFile
public static void Main(string args)
string fileName = "input.txt";
int wordsCount = 0;
using (StreamReader file = File.OpenText(fileName))
line = file.ReadLine();
if (line != null)
wordsCount += line.Split(' ').Length;
while (line != null);
Practice C# anywhere with the free app for Android devices.
Learn C# at your own pace, the exercises are ordered by difficulty.
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.