Write a C# program to count a total number of vowel or consonant 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 i, length, vowel, consonant;
  8.  
  9. Console.Write("Enter the string : ");
  10. str = Console.ReadLine();
  11.  
  12. vowel = 0;
  13. consonant = 0;
  14. length = str.Length;
  15.  
  16. for (i = 0; i < length; i++)
  17. {
  18.  
  19. if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U')
  20. {
  21. vowel++;
  22. }
  23. else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
  24. {
  25. consonant++;
  26. }
  27. }
  28. Console.Write("\nThe total number of vowel in the string is : {0}\n", vowel);
  29. Console.Write("The total number of consonant in the string is : {0}\n\n", consonant);
  30.  
  31. Console.ReadLine();
  32. }
  33. }

Result

C# language techstudy

     





Related Topics




Popular Posts