In this example, 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.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | static void Main(string[] args) { Console.WriteLine("--------------------------------------------------------"); Console.WriteLine("Program to count of letter, digit and special characters "); Console.WriteLine("--------------------------------------------------------"); Console.Write("Enter a string :"); string str = Console.ReadLine(); int totalCharacter = 0, totalLetterChar = 0, totalDigitChar = 0, totalSpecialChar = 0; char[] strArray = str.ToCharArray(); foreach (var item in strArray) { if (char.IsDigit(item)) totalDigitChar++; if (char.IsLetter(item)) totalLetterChar++; if (!char.IsLetterOrDigit(item)) totalSpecialChar++; totalCharacter++; } Console.WriteLine("----------------------------------------------"); Console.WriteLine("Entered String : " + str); Console.WriteLine("----------------------------------------------"); Console.WriteLine("Total Characters In String : " + totalCharacter); Console.WriteLine("Total Letter Character String : " + totalLetterChar); Console.WriteLine("Total Digit Character String : " + totalDigitChar); Console.WriteLine("Total Specia Character String : " + totalSpecialChar); Console.WriteLine("----------------------------------------------"); Console.ReadKey(); } |
