The following application displays the number of words in the string that's input at the console. It does that by counting the number of space characters, and then adding 1 to them, thus getting the correct number of words. Here's how the C# code looks.
using System;
namespace WordCounter
{
class Program
{
static void Main(string[] args)
{
string input;
int spaces = 0;
int words = 0;
Console.WriteLine("Enter a string: ");
input = Console.ReadLine();
for (int i = 0; i < input.Length; i++)
{
if (input[i] == ' ')
{
spaces++;
}
}
words = spaces + 1;
Console.WriteLine("The number of words is: " + words);
}
}
}
And here's the code in action.