Write a C# program to count a total number of alphabets, digits and special characters in a string

Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..

using System;
public class StringExercise
{
    public static void Main()
    {
        string str;
        int alphabet, digit, specialchar, i, l;
        alphabet = digit = specialchar = i = 0;
 
        Console.Write("Enter the string : ");
        str = Console.ReadLine();
        l = str.Length;
 
        while (i < l)
        {
            if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
            {
                alphabet++;
            }
            else if (str[i] >= '0' && str[i] <= '9')
            {
                digit++;
            }
            else
            {
                specialchar++;
            }
 
            i++;
        }
 
        Console.Write("Number of Alphabets in the string is : {0}\n", alphabet);
        Console.Write("Number of Digits in the string is : {0}\n", digit);
        Console.Write("Number of Special characters in the string is : {0}\n\n", specialchar);
 
        Console.ReadLine();
    }
}

Result

Write a C# program to count a total number of alphabets, digits and special characters in a string
Write a C# program to count a total number of alphabets, digits and special characters in a string

Leave a Comment

x