Showing posts with label Best method to find out space in a string in c#. Show all posts
Showing posts with label Best method to find out space in a string in c#. Show all posts

Friday, February 20, 2015

How we can find that – How many spaces and characters are there in a String

There is number of ways through which  we can achieve the above task, But I am going to tell you that How we can achieve it in optimize manner.

Example – There is a string-    "Rahul Yadav Dot Net Developer" , We need to find out how many spaces and characters are there ?

Program in C#-


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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {

            Rahul obj = new Rahul();
            obj.Find_Char();

        }
    }

   class Rahul
    {

        public void Find_Char()
        {

            int k = 0;
            int j = 0;

           string Name = "Rahul Yadav Dot Net Developer";
           foreach (Char g in Name)
            {

                if (char.IsWhiteSpace(g))
                {

                    k = k + 1;

                }


               else
                {

                    j = j + 1;
               
               
                }
           
            }
            Console.WriteLine("Number Of Spaces :" + k);
            Console.Write("Number Of Characters :" + j);
            Console.ReadLine();

            }
      
    }
}


Output:
                                   
                                             Author- Er. Rahul Kr. Yadav