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


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. public class StringExercise
  3. {
  4. public static void Main()
  5. {
  6. string str;
  7. int alphabet, digit, specialchar, i, l;
  8. alphabet = digit = specialchar = i = 0;
  9.  
  10. Console.Write("Enter the string : ");
  11. str = Console.ReadLine();
  12. l = str.Length;
  13.  
  14. while (i < l)
  15. {
  16. if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
  17. {
  18. alphabet++;
  19. }
  20. else if (str[i] >= '0' && str[i] <= '9')
  21. {
  22. digit++;
  23. }
  24. else
  25. {
  26. specialchar++;
  27. }
  28.  
  29. i++;
  30. }
  31.  
  32. Console.Write("Number of Alphabets in the string is : {0}\n", alphabet);
  33. Console.Write("Number of Digits in the string is : {0}\n", digit);
  34. Console.Write("Number of Special characters in the string is : {0}\n\n", specialchar);
  35.  
  36. Console.ReadLine();
  37. }
  38. }

Result

C# language techstudy

     





Related Topics




Popular Posts