Count Character in a string - CSharp Language Basics

CSharp examples for Language Basics:string

Description

Count Character in a string

Demo Code

using System;/*from w  ww  . ja  va2 s.c  o m*/
class CharacterCounter
{
   public static void Main()
   {
      string myText;
      int charCount = 0;
      char ch;
      int index = 0;
      Console.WriteLine("Enter some text");
      myText = Console.ReadLine();
      while (index < myText.Length)
      {
         ch = myText[index];
         if (!char.IsWhiteSpace(ch))
            charCount++;
         index++;
      }
      Console.WriteLine("Number of non-white-space characters: {0:N0}", charCount);
   }
}

Result


Related Tutorials