Count Total Letters, Digits And Special Characters From String In C#

Introduction

 
In this blog, we create a C# program that takes a string as an input, and then we give output as how many characters are in that string, how many characters are letters, how many characters are digits and how many characters are is a special character like symbol, space, etc.
 
Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace CountTotalNumberInString  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Console.WriteLine("--------------------------------------------------------");  
  14.             Console.WriteLine("Program to count of letter, digit and special characters ");  
  15.             Console.WriteLine("--------------------------------------------------------");  
  16.             Console.Write("Enter a string :");  
  17.             string str = Console.ReadLine();  
  18.   
  19.             int totalCharacter = 0, totalLetterChar = 0, totalDigitChar = 0, totalSpecialChar = 0;  
  20.   
  21.             char[] strArray = str.ToCharArray();  
  22.             foreach (var item in strArray)  
  23.             {  
  24.                 if (char.IsDigit(item))  
  25.                     totalDigitChar++;  
  26.                 if (char.IsLetter(item))  
  27.                     totalLetterChar++;  
  28.                 if (!char.IsLetterOrDigit(item))  
  29.                     totalSpecialChar++;  
  30.                 totalCharacter++;  
  31.             }  
  32.   
  33.             Console.WriteLine("--------------------------------------------------------");  
  34.             Console.WriteLine("Entered String : "+str);  
  35.             Console.WriteLine("--------------------------------------------------------");  
  36.             Console.WriteLine("Total Characters In String : "+totalCharacter);  
  37.             Console.WriteLine("Total Letter Character String : "+totalLetterChar);  
  38.             Console.WriteLine("Total Digit Character String : "+totalDigitChar);  
  39.             Console.WriteLine("Total Specia Character String : "+totalSpecialChar);  
  40.             Console.WriteLine("--------------------------------------------------------");  
  41.             Console.ReadKey();  
  42.         }  
  43.     }  
  44. }  
Output
 
Count Total Letters, Digits And Special Characters From String
 
Explanation 
  • Here first we get input from the user.
  • Then define some integer variables which we used as a counter.
  • Then we convert the input string as a char array by ToCharArray() method.
  • Then iterate for each loop on char array
    • In this loop, we check if character is a letter or digit by char.IsLetter() and char.IsDigit() method and increment in our counter variable.
    • And for a special character, we use char.IsLetterOrDigit() method this method return true if the character is a digit or letter and false if not. We increment our counter variable if the condition is false.