Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Count a total number of vowel or consonant

C# Sharp String: Exercise-9 with Solution

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

C# Sharp Exercises: Count a total number of vowel or consonant.

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise9  
{  
    public static void Main() 
{
    string str;
    int i, len, vowel, cons;
	
      Console.Write("\n\nCount total number of vowel or consonant :\n");
      Console.Write("----------------------------------------------\n"); 	
      Console.Write("Input the string : ");
      str = Console.ReadLine();		

    vowel = 0;
    cons = 0;
    len = str.Length;

    for(i=0; i<len; 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'))
        {
            cons++;
        }
    }
   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", cons);
	}
}

Sample Output:

Count total number of vowel or consonant :                             
----------------------------------------------                         
Input the string : Welcome to w3resource.com                           
                                                                       
The total number of vowel in the string is : 9                         
The total number of consonant in the string is : 12 

Flowchart:

Flowchart: Count total number of vowel or consonant

C# Sharp Practice online:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to copy one string to another string.
Next: Write a program in C# Sharp to find maximum occurring character in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.