Author Topic: C# program to count number of alphabets and non-alphabets in a string  (Read 1397 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Here is the C# program to count number of alphabets and non-alphabets in a string.

Code: (c#)
//Source Code Listing

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

namespace CSTest
{  
   class CharCount
   {

      static void Main(string[] args)
      {
         System.Console.WriteLine("Program to count the number of alphabets and non-alphabets");
         System.Console.Write("Enter a string:");
         string input = System.Console.ReadLine();

         int alphabet_count = 0;
         int non_alphabet_count = 0;

         for(int i = 0; i < input.Length; i++)
         {
            if(input[i] >= 'a' && input[i] <= 'z')
               alphabet_count++;
            else if(input[i] >= 'A' && input[i] <= 'Z')
               alphabet_count++;
            else 
               non_alphabet_count++;

         }
         
         System.Console.WriteLine("Number of alphabets: {0}", alphabet_count);
         System.Console.WriteLine("Number of Non-alphabets: {0}", non_alphabet_count);

      }
   }
}

output:

Program to count the number of alphabets and non-alphabets
Enter a string:welcome to software & finance
Number of alphabets: 24
Number of Non-alphabets: 5

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1