9 October 2013

C# program to count number of spaces and words in a entered string

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = null;
            char sp = ' ';
            int i, count;
            count = 0;
            Console.WriteLine("Enter a string:");
            s1 = Console.ReadLine();
            for (i = 0; i < s1.Length; i++)
            {
                if (s1[i].Equals(sp))
                {
                    count++;
                }
            }
            Console.WriteLine("Number of spaces=" + count);
            Console.WriteLine("Number of words are=" + (count + 1));
            Console.ReadLine();
        }
    }
}



No comments:

Post a Comment