Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

Enter the string : @techstudy99

Number of Alphabets in the string is : 9

Number of Digits in the string is : 2

Number of Special characters in the string is : 1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a C# program to print individual characters ... >>
<< Write a C# program to find the length of a string ...