Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Count a total number of alphabets, digits and special characters

C# Sharp String: Exercise-7 with Solution

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

C# Sharp Exercises: Count a total number of alphabets, digits and special characters.

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise7  
{  
    public static void Main() 
{
    string str;
    int alp, digit, splch, i,l;
    alp = digit = splch = i = 0;


      Console.Write("\n\nCount total number of alphabets, digits and special characters :\n");
      Console.Write("--------------------------------------------------------------------\n"); 	
      Console.Write("Input the string : ");
      str = Console.ReadLine();	
      l=str.Length; 

     /* Checks each character of string*/

    while(i<l)
    {
        if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
        {
            alp++;
        }
        else if(str[i]>='0' && str[i]<='9')
        {
            digit++;
        }
        else
        {
            splch++;
        }

        i++;
    }

   Console.Write("Number of Alphabets in the string is : {0}\n", alp);
   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", splch);
	}
}

Sample Output:

Count total number of alphabets, digits and special characters :       
--------------------------------------------------------------------   
Input the string : Welcome to w3resource.com                           
Number of Alphabets in the string is : 21                              
Number of Digits in the string is : 1                                  
Number of Special characters in the string is : 3 

Flowchart:

Flowchart: Count total number of alphabets, digits and special characters

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to compare two string without using string library functions.
Next: Write a program in C# Sharp to copy one string to another string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.