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

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to count a total number of alphabets, digits and special characters in a string. 
Test Data :
Input the string : Welcome to w3resource.com
Expected Output :

Number of Alphabets in the string is : 21 
Number of Digits in the string is : 1 
Number of Special characters in the string is : 4 

الأجوبة

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);
	}
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...