Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

Enter the string : techstudy.org

The total number of vowel in the string is : 3

The total number of consonant in the string is : 9

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a C# program to find maximum occurring chara... >>
<< Write a C# program to copy one string to another s...