Q:
C# program to count the total number of vowels in a given string
belongs to collection: C# Basic Programs | String programs
C# Basic Programs | String programs
- Comparing two strings in C#
- Demonstrate the example of Copy method of String class in C#
- Demonstrate the example of IndexOf() method of string class in C#
- Compare strings using Equals() method in C#
- Explain LastIndexOf() method of String class with Example in C#
- Explain String.Split() method of String class in C# with an Example
- How to convert string into uppercase in C#?
- How to convert string into lowercase in C#?
- How to get substring from a string in C#?
- How to trim the string in C#, an example of String.Trim() method?
- How to trim leading spaces of string using String.TrimStart() in C#?
- How to trim trailing spaces of string using String.TrimEnd() in C#?
- How to pad string from left using String.PadLeft() in C#?
- How to pad string from right using String.PadRight() in C#?
- Check whether string ends with given substring or not using String.EndsWith() in C#?
- How to remove given substring from a string using String.Remove() in C#?
- How to replace a character with another character in a string in C#?
- C# program to count the frequency of the specified word in the given string
- C# program to trim a specified string
- C# program to trim a specified string from the left side
- C# program to trim a specified string from the right side
- C# program to split a string using the Split() method of String class
- C# program to extract only numbers from a specified string using the Split() method
- C# program to get the length of the string
- C# program to replace a substring within a specified string
- C# program to find the occurrence of the specified word in a given string
- C# program to convert a string from lowercase to uppercase
- C# program to print the abbreviation of a given string
- C# program to print the list of all possible substrings of a specified string
- C# program to count the total number of digits in an alpha-numeric string
- C# program to concatenate the two strings using a predefined method
- C# program to reverse a given string without using the predefined method
- C# program to perform left padding without using PadLeft() method
- C# program to perform the right padding without using the PadRight() method
- C# program to count the total number of vowels in a given string
- C# program to generate random strings
- C# program to encrypt and decrypt a string using Rijndael key algorithm
- C# program to count the lines in a given string
Program:
The source code to count the total number of vowels in a given string is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
Here, we created a class Demo that contains two static methods CountVowels() and Main().
In the CountVowels(), we traversed the string character by character and check the character is a vowel, if any character found a vowel then we increase the value of the variable countVowels by 1, after traversing the complete string we returned the value of countVowels variable to the Main() method.
In the Main() method, we created a string str and then read a string from the user and passed the string str to the CountVowels() method that will return the count of vowels and then we printed the return value on the console screen.