查找字符是元音还是辅音的程序
给定一个字符,检查它是元音还是辅音。元音是“a”、“e”、“i”、“o”和“u”。所有其他字符('b'、'c'、'd'、'f' ....)都是辅音。
例子:
Input : x = 'c'
Output : Consonant
Input : x = 'u'
Output : Vowel
我们检查给定字符是否匹配 5 个元音中的任何一个。如果是,我们打印“元音”,否则我们打印“辅音”。
C++
// CPP program to check if a given character
// is vowel or consonant.
#include
using namespace std;
// Function to check whether a character is
// vowel or not
void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u')
cout << "Vowel" << endl;
else
cout << "Consonant" << endl
}
// Driver code
int main()
{
vowelOrConsonant('c');
vowelOrConsonant('e');
return 0;
}
Java
// java program to check if a given
// character is vowel or consonant.
import java.io.*;
public class GFG {
// Function to check whether a
// character is vowel or not
static void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u')
System.out.println("Vowel");
else
System.out.println("Consonant");
}
// Driver code
static public void main(String[] args)
{
vowelOrConsonant('c');
vowelOrConsonant('e');
}
}
// This code is contributed by vt_m.
Python3
# Python3 program to check if a given
# character is vowel or consonant.
# Function to check whether a character
# is vowel or not
def vowelOrConsonant(x):
if (x == 'a' or x == 'e' or
x == 'i' or x == 'o' or x == 'u'):
print("Vowel")
else:
print("Consonant")
# Driver code
vowelOrConsonant('c')
vowelOrConsonant('e')
# This code is contributed by
# mohit kumar 29
C#
// C# program to check if a given
// character is vowel or consonant.
using System;
public class GFG {
// Function to check whether a
// character is vowel or not
static void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u')
Console.WriteLine("Vowel");
else
Console.WriteLine("Consonant");
}
// Driver code
static public void Main()
{
vowelOrConsonant('c');
vowelOrConsonant('e');
}
}
// This code is contributed by vt_m.
PHP
Javascript
C++
// C++ program to check if a given character
// is vowel or consonant.
#include
using namespace std;
// Function to check whether a character is
// vowel or not
void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
cout << "Vowel" << endl;
else
cout << "Consonant" << endl;
}
// Driver code
int main()
{
vowelOrConsonant('c');
vowelOrConsonant('E');
return 0;
}
Java
// Java program to check if a given
// character is vowel or consonant
import java.io.*;
public class GFG {
// Function to check whether a
// character is vowel or not
static void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
System.out.println("Vowel");
else
System.out.println("Consonant");
}
// Driver code
static public void main(String[] args)
{
vowelOrConsonant('c');
vowelOrConsonant('E');
}
}
// This article is contributed by vt_m.
Python3
# Python3 program to check if a given
# character is vowel or consonant.
# Function to check whether a
# character is vowel or not
def vowelOrConsonant(x):
if (x == 'a' or x == 'e' or x == 'i' or
x == 'o' or x == 'u' or x == 'A' or
x == 'E' or x == 'I' or x == 'O' or
x == 'U'):
print("Vowel")
else:
print("Consonant")
# Driver code
if __name__ == '__main__':
vowelOrConsonant('c')
vowelOrConsonant('E')
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to check if a given
// character is vowel or consonant
using System;
public class GFG {
// Function to check whether a
// character is vowel or not
static void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
Console.WriteLine("Vowel");
else
Console.WriteLine("Consonant");
}
// Driver code
static public void Main()
{
vowelOrConsonant('c');
vowelOrConsonant('E');
}
}
// This article is contributed by vt_m.
PHP
Javascript
C++
#include
using namespace std;
int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
check = 1;
}
return check;
}
// Driver Code
int main()
{
cout << "a is " << isVowel('a')
<< endl; // 1 means Vowel
cout << "x is " << isVowel('x')
<< endl; // 0 means Consonant
return 0;
}
C
#include
int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
check = 1;
}
return check;
}
int main()
{
printf("a is %d\n",
isVowel('a')); // 1 means Vowel
printf("x is %d\n",
isVowel('x')); // 0 means Consonant
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
check = 1;
}
return check;
}
// Driver Code
public static void main(String[] args)
{
System.out.println("a is " + isVowel('a'));
System.out.println("x is " + isVowel('x'));
}
}
Python3
def isVowel(ch):
switcher = {
'a': "Vowel",
'e': "Vowel",
'i': "Vowel",
'o': "Vowel",
'u': "Vowel",
'A': "Vowel",
'E': "Vowel",
'I': "Vowel",
'O': "Vowel",
'U': "Vowel"
}
return switcher.get(ch, "Consonant")
# Driver Code
print('a is '+isVowel('a'))
print('x is '+isVowel('x'))
C#
using System;
class GFG
{
static int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
check = 1;
break;
case 'e':
check = 1;
break;
case 'i':
check = 1;
break;
case 'o':
check = 1;
break;
case 'u':
check = 1;
break;
case 'A':
check = 1;
break;
case 'E':
check = 1;
break;
case 'I':
check = 1;
break;
case 'O':
check = 1;
break;
case 'U':
check = 1;
break;
}
return check;
}
// Driver code
static public void Main ()
{
Console.WriteLine("a is " + isVowel('a'));
Console.WriteLine("x is " + isVowel('x'));
}
}
// This code is contributed by rag2127
Javascript
C++
#include
#include
using namespace std;
int isVowel(char ch)
{
// Make the list of vowels
string str = "aeiouAEIOU";
return (str.find(ch) != string::npos);
}
// Driver code
int main()
{
cout << "a is " << isVowel('a') << endl;
cout << "x is " << isVowel('x') << endl;
return 0;
}
C
#include
#include
int isVowel(char ch)
{
// Make the list of vowels
char str[] = "aeiouAEIOU";
return (strchr(str, ch) != NULL);
}
// Driver Code
int main()
{
printf("a is %d\n", isVowel('a'));
printf("x is %d\n", isVowel('x'));
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static int isVowel(char ch)
{
// Make the list of vowels
String str = "aeiouAEIOU";
return (str.indexOf(ch) != -1) ? 1 : 0;
}
// Driver Code
public static void main(String[] args)
{
System.out.println("a is " + isVowel('a'));
System.out.println("x is " + isVowel('x'));
}
}
Python3
def isVowel(ch):
# Make the list of vowels
str = "aeiouAEIOU"
return (str.find(ch) != -1)
# Driver Code
print('a is '+str(isVowel('a')))
print('x is '+str(isVowel('x')))
C#
// C# program to implement
// the above approach
using System;
class GFG{
static int isVowel(char ch)
{
// Make the list of vowels
string str = "aeiouAEIOU";
return (str.IndexOf(ch) != -1) ?
1 : 0;
}
// Driver code
static void Main()
{
Console.WriteLine("a is " + isVowel('a'));
Console.WriteLine("x is " + isVowel('x'));
}
}
// This code is contributed by divyeshrabadiya07
Javascript
C++
#include
using namespace std;
int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver Code
int main()
{
cout << "a is " << isVowel('a') << endl;
cout << "x is " << isVowel('x') << endl;
return 0;
}
C
#include
int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver Code
int main()
{
printf("a is %d\n", isVowel('a'));
printf("x is %d\n", isVowel('x'));
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
static int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver Code
public static void main(String[] args)
{
System.out.println("a is " + isVowel('a'));
System.out.println("x is " + isVowel('x'));
}
}
Python3
def isVowel(ch):
return (0x208222 >> (ord(ch) & 0x1f)) & 1
# same as (2130466 >> (ord(ch) & 31)) & 1;
# Driver Code
print('a is '+str(isVowel('a')))
print('x is '+str(isVowel('x')))
C#
// C# implementation of above approach
using System;
class GFG {
static int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver code
static void Main()
{
Console.WriteLine("a is " + isVowel('a'));
Console.WriteLine("x is " + isVowel('x'));
}
}
// This code is contributed by divesh072019
Javascript
输出:
Consonant
Vowel
时间复杂度: O(1)
辅助空间: O(1)
如何处理大写字母?
C++
// C++ program to check if a given character
// is vowel or consonant.
#include
using namespace std;
// Function to check whether a character is
// vowel or not
void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
cout << "Vowel" << endl;
else
cout << "Consonant" << endl;
}
// Driver code
int main()
{
vowelOrConsonant('c');
vowelOrConsonant('E');
return 0;
}
Java
// Java program to check if a given
// character is vowel or consonant
import java.io.*;
public class GFG {
// Function to check whether a
// character is vowel or not
static void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
System.out.println("Vowel");
else
System.out.println("Consonant");
}
// Driver code
static public void main(String[] args)
{
vowelOrConsonant('c');
vowelOrConsonant('E');
}
}
// This article is contributed by vt_m.
Python3
# Python3 program to check if a given
# character is vowel or consonant.
# Function to check whether a
# character is vowel or not
def vowelOrConsonant(x):
if (x == 'a' or x == 'e' or x == 'i' or
x == 'o' or x == 'u' or x == 'A' or
x == 'E' or x == 'I' or x == 'O' or
x == 'U'):
print("Vowel")
else:
print("Consonant")
# Driver code
if __name__ == '__main__':
vowelOrConsonant('c')
vowelOrConsonant('E')
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to check if a given
// character is vowel or consonant
using System;
public class GFG {
// Function to check whether a
// character is vowel or not
static void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
Console.WriteLine("Vowel");
else
Console.WriteLine("Consonant");
}
// Driver code
static public void Main()
{
vowelOrConsonant('c');
vowelOrConsonant('E');
}
}
// This article is contributed by vt_m.
PHP
Javascript
输出:
Consonant
Vowel
时间复杂度: O(1)
辅助空间: O(1)
使用开关盒
C++
#include
using namespace std;
int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
check = 1;
}
return check;
}
// Driver Code
int main()
{
cout << "a is " << isVowel('a')
<< endl; // 1 means Vowel
cout << "x is " << isVowel('x')
<< endl; // 0 means Consonant
return 0;
}
C
#include
int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
check = 1;
}
return check;
}
int main()
{
printf("a is %d\n",
isVowel('a')); // 1 means Vowel
printf("x is %d\n",
isVowel('x')); // 0 means Consonant
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
check = 1;
}
return check;
}
// Driver Code
public static void main(String[] args)
{
System.out.println("a is " + isVowel('a'));
System.out.println("x is " + isVowel('x'));
}
}
Python3
def isVowel(ch):
switcher = {
'a': "Vowel",
'e': "Vowel",
'i': "Vowel",
'o': "Vowel",
'u': "Vowel",
'A': "Vowel",
'E': "Vowel",
'I': "Vowel",
'O': "Vowel",
'U': "Vowel"
}
return switcher.get(ch, "Consonant")
# Driver Code
print('a is '+isVowel('a'))
print('x is '+isVowel('x'))
C#
using System;
class GFG
{
static int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
check = 1;
break;
case 'e':
check = 1;
break;
case 'i':
check = 1;
break;
case 'o':
check = 1;
break;
case 'u':
check = 1;
break;
case 'A':
check = 1;
break;
case 'E':
check = 1;
break;
case 'I':
check = 1;
break;
case 'O':
check = 1;
break;
case 'U':
check = 1;
break;
}
return check;
}
// Driver code
static public void Main ()
{
Console.WriteLine("a is " + isVowel('a'));
Console.WriteLine("x is " + isVowel('x'));
}
}
// This code is contributed by rag2127
Javascript
a is 1
x is 0
时间复杂度: O(1)
辅助空间: O(1)
另一种方法是 find()字符中仅包含元音的字符串。
C++
#include
#include
using namespace std;
int isVowel(char ch)
{
// Make the list of vowels
string str = "aeiouAEIOU";
return (str.find(ch) != string::npos);
}
// Driver code
int main()
{
cout << "a is " << isVowel('a') << endl;
cout << "x is " << isVowel('x') << endl;
return 0;
}
C
#include
#include
int isVowel(char ch)
{
// Make the list of vowels
char str[] = "aeiouAEIOU";
return (strchr(str, ch) != NULL);
}
// Driver Code
int main()
{
printf("a is %d\n", isVowel('a'));
printf("x is %d\n", isVowel('x'));
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static int isVowel(char ch)
{
// Make the list of vowels
String str = "aeiouAEIOU";
return (str.indexOf(ch) != -1) ? 1 : 0;
}
// Driver Code
public static void main(String[] args)
{
System.out.println("a is " + isVowel('a'));
System.out.println("x is " + isVowel('x'));
}
}
Python3
def isVowel(ch):
# Make the list of vowels
str = "aeiouAEIOU"
return (str.find(ch) != -1)
# Driver Code
print('a is '+str(isVowel('a')))
print('x is '+str(isVowel('x')))
C#
// C# program to implement
// the above approach
using System;
class GFG{
static int isVowel(char ch)
{
// Make the list of vowels
string str = "aeiouAEIOU";
return (str.IndexOf(ch) != -1) ?
1 : 0;
}
// Driver code
static void Main()
{
Console.WriteLine("a is " + isVowel('a'));
Console.WriteLine("x is " + isVowel('x'));
}
}
// This code is contributed by divyeshrabadiya07
Javascript
a is 1
x is 0
时间复杂度: O(N)
辅助空间: O(1)
使用位移检查元音的最有效方法:
在 ASCII 中,这些是每个元音在小写和大写中的各自值。 a e i o u A E I O UVowel DEC HEX BINARY 97 0x61 01100001 101 0x65 01100101 105 0x69 01101001 111 0x6F 01101111 117 0x75 01110101 65 0x41 01000001 69 0x45 01000101 73 0x49 01001001 79 0x4F 01001111 85 0x55 01010101
因为非常小写和大写的元音具有相同的 5 个 LSB。我们需要一个数字 0x208222,它在右移 1、5、19、15 后的 LSB 中给出 1,否则给出 0。数字取决于字符编码。
DEC HEX BINARY 31 0x1F 00011111 2130466 0x208222 1000001000001000100010
C++
#include
using namespace std;
int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver Code
int main()
{
cout << "a is " << isVowel('a') << endl;
cout << "x is " << isVowel('x') << endl;
return 0;
}
C
#include
int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver Code
int main()
{
printf("a is %d\n", isVowel('a'));
printf("x is %d\n", isVowel('x'));
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
static int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver Code
public static void main(String[] args)
{
System.out.println("a is " + isVowel('a'));
System.out.println("x is " + isVowel('x'));
}
}
Python3
def isVowel(ch):
return (0x208222 >> (ord(ch) & 0x1f)) & 1
# same as (2130466 >> (ord(ch) & 31)) & 1;
# Driver Code
print('a is '+str(isVowel('a')))
print('x is '+str(isVowel('x')))
C#
// C# implementation of above approach
using System;
class GFG {
static int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver code
static void Main()
{
Console.WriteLine("a is " + isVowel('a'));
Console.WriteLine("x is " + isVowel('x'));
}
}
// This code is contributed by divesh072019
Javascript
a is 1
x is 0
时间复杂度: O(1)
辅助空间: O(1)
*我们可以在 X86 机器上省略 ( ch & 0x1f ) 部分,因为 SHR/SAR (即 >> ) 自动掩码为 0x1f。
*对于机器来说,位图检查比表格检查快,但如果 ch 变量存储在寄存器中,它可能会执行得更快。