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..

using System;
public class StringExercise
{
    public static void Main()
    {
        string str;
        int i, length, vowel, consonant;
 
        Console.Write("Enter the string : ");
        str = Console.ReadLine();
 
        vowel = 0;
        consonant = 0;
        length = str.Length;
 
        for (i = 0; i < length; i++)
        {
 
            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')
            {
                vowel++;
            }
            else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
            {
                consonant++;
            }
        }
        Console.Write("\nThe total number of vowel in the string is : {0}\n", vowel);
        Console.Write("The total number of consonant in the string is : {0}\n\n", consonant);
 
        Console.ReadLine();
    }
}

Result

Write a C# program to count a total number of vowel or consonant in a string
Write a C# program to count a total number of vowel or consonant in a string

Leave a Comment