C# 程序计算给定字符串中元音和辅音的数量
C# 是一种通用编程语言,用于创建移动应用程序、桌面应用程序、Web
网站和游戏。我们知道 a, e, i, o, u 是元音,其余的字母在英语中称为辅音,所以现在使用 C# 语言创建一个程序,该程序将返回元音和辅音的总数给定的字符串。
例子:
Input: geeksforgeeks
Output: Total number of vowels = 5
Total number of consonants = 8
Input: HelloGFG
Output: Total number of vowels = 2
Total number of consonants = 6
方法:
To print the total number of Vowels and consonants from a given String we use the following approach:
- Store the string using string datatype.
- Declare two variables to count the number of vowels and consonants.
- Now using the length property find the length of the given string
- Now iterate the string from left to right and check if the character is either vowel or a consonant.
- If the character encountered is a vowel increase the count of vowel else increases the count of consonant.
示例 1:
C#
// C# program to print the total number of Vowels
// and consonants from a given string
using System;
class GFG{
public static void Main()
{
string inputstring;
int i, len, vowels, consonants;
inputstring = "geeksforgeeks";
vowels = 0;
consonants = 0;
len = inputstring.Length;
// Iterating the string from left to right
for(i = 0; i < len; i++)
{
// Check if the character is a vowel
if (inputstring[i] == 'a' || inputstring[i] == 'e' ||
inputstring[i] == 'i' || inputstring[i] == 'o' ||
inputstring[i] == 'u' || inputstring[i] == 'A' ||
inputstring[i] == 'E' || inputstring[i] == 'I' ||
inputstring[i] == 'O' || inputstring[i] == 'U')
{
// Increment the vowels
vowels++;
}
// Check if the character is a alphabet
// other than vowels
else if ((inputstring[i] >= 'a' && inputstring[i] <= 'z') ||
(inputstring[i] >= 'A' && inputstring[i] <= 'Z'))
{
// Increment the consonants
consonants++;
}
}
// Display the count of vowels and consonant
Console.WriteLine("count of vowel = " + vowels);
Console.WriteLine("count of consonant = " + consonants);
}
}
C#
// C# program to print the total number of Vowels
// and consonants from a given string
using System;
class GFG{
public static void Main()
{
char[] inputstring = new char[100];
int i, vowels, consonants, x;
vowels = 0;
consonants = 0;
// Enter the length of the string
Console.WriteLine("Please enter the length of the string:\n");
x = int.Parse(Console.ReadLine());
// Enter the string
Console.WriteLine("Enter string:\n");
for (i = 0; i < x; i++)
{
inputstring[i] = Convert.ToChar(Console.Read());
}
// Iterating the string
for (i = 0; inputstring[i] != '\0'; i++)
{
// Check if the character is a vowel
if (inputstring[i] == 'a' || inputstring[i] == 'e' ||
inputstring[i] == 'i' || inputstring[i] == 'o' ||
inputstring[i] == 'u' || inputstring[i] == 'A' ||
inputstring[i] == 'E' || inputstring[i] == 'I' ||
inputstring[i] == 'O' || inputstring[i] == 'U')
{
// Increment the vowels
vowels++;
}
else
{
// Increment the consonants
consonants++;
}
}
// Display the count of vowels and consonant
Console.WriteLine("\ncount of vowel = " + vowels);
Console.WriteLine("count of consonant = " + consonants);
Console.ReadLine();
Console.ReadLine();
}
}
输出
count of vowel = 5
count of consonant = 8
示例 2:
C#
// C# program to print the total number of Vowels
// and consonants from a given string
using System;
class GFG{
public static void Main()
{
char[] inputstring = new char[100];
int i, vowels, consonants, x;
vowels = 0;
consonants = 0;
// Enter the length of the string
Console.WriteLine("Please enter the length of the string:\n");
x = int.Parse(Console.ReadLine());
// Enter the string
Console.WriteLine("Enter string:\n");
for (i = 0; i < x; i++)
{
inputstring[i] = Convert.ToChar(Console.Read());
}
// Iterating the string
for (i = 0; inputstring[i] != '\0'; i++)
{
// Check if the character is a vowel
if (inputstring[i] == 'a' || inputstring[i] == 'e' ||
inputstring[i] == 'i' || inputstring[i] == 'o' ||
inputstring[i] == 'u' || inputstring[i] == 'A' ||
inputstring[i] == 'E' || inputstring[i] == 'I' ||
inputstring[i] == 'O' || inputstring[i] == 'U')
{
// Increment the vowels
vowels++;
}
else
{
// Increment the consonants
consonants++;
}
}
// Display the count of vowels and consonant
Console.WriteLine("\ncount of vowel = " + vowels);
Console.WriteLine("count of consonant = " + consonants);
Console.ReadLine();
Console.ReadLine();
}
}
输出:
Please enter the length of the string:
6
Enter string:
HeyGFG
count of vowel = 1
count of consonant = 5