给定由字母组成的字符串str ,任务是检查给定的字符串是否以元音开头。
例子:
Input: str = "Animal"
Output: Accepted
Input: str = "GeeksforGeeks"
Output: Not Accepted
方法:
- 查找字符串的第一个字符
- 检查字符串的第一个字符是否为元音
- 如果是,打印 Accepted
- 不接受其他打印
下面是上述方法的实现:
CPP
// C++ program to accept String
// starting with Vowel
#include
using namespace std;
// Function to check if first character is vowel
int checkIfStartsWithVowels(string str)
{
if (!(str[0] == 'A' || str[0] == 'a'
|| str[0] == 'E' || str[0] == 'e'
|| str[0] == 'I' || str[0] == 'i'
|| str[0] == 'O' || str[0] == 'o'
|| str[0] == 'U' || str[0] == 'u'))
return 1;
else
return 0;
}
// Function to check
void check(string str)
{
if (checkIfStartsWithVowels(str))
cout << "Not Accepted\n";
else
cout << "Accepted\n";
}
// Driver function
int main()
{
string str = "animal";
check(str);
str = "zebra";
check(str);
return 0;
}
Java
// Java program to accept String
// starting with Vowel
class GFG
{
// Function to check if first character is vowel
static int checkIfStartsWithVowels(char []str)
{
if (!(str[0] == 'A' || str[0] == 'a'
|| str[0] == 'E' || str[0] == 'e'
|| str[0] == 'I' || str[0] == 'i'
|| str[0] == 'O' || str[0] == 'o'
|| str[0] == 'U' || str[0] == 'u'))
return 1;
else
return 0;
}
// Function to check
static void check(String str)
{
if (checkIfStartsWithVowels(str.toCharArray()) == 1)
System.out.print("Not Accepted\n");
else
System.out.print("Accepted\n");
}
// Driver code
public static void main(String[] args)
{
String str = "animal";
check(str);
str = "zebra";
check(str);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to accept String
# starting with Vowel
# Function to check if first character is vowel
def checkIfStartsWithVowels(string) :
if (not(string[0] == 'A' or string[0] == 'a'
or string[0] == 'E' or string[0] == 'e'
or string[0] == 'I' or string[0] == 'i'
or string[0] == 'O' or string[0] == 'o'
or string[0] == 'U' or string[0] == 'u')) :
return 1;
else :
return 0;
# Function to check
def check(string) :
if (checkIfStartsWithVowels(string)) :
print("Not Accepted");
else :
print("Accepted");
# Driver function
if __name__ == "__main__" :
string = "animal";
check(string);
string = "zebra";
check(string);
# This code is contributed by AnkitRai01
C#
// C# program to accept String
// starting with Vowel
using System;
class GFG
{
// Function to check if first character is vowel
static int checkIfStartsWithVowels(char []str)
{
if (!(str[0] == 'A' || str[0] == 'a'
|| str[0] == 'E' || str[0] == 'e'
|| str[0] == 'I' || str[0] == 'i'
|| str[0] == 'O' || str[0] == 'o'
|| str[0] == 'U' || str[0] == 'u'))
return 1;
else
return 0;
}
// Function to check
static void check(String str)
{
if (checkIfStartsWithVowels(str.ToCharArray()) == 1)
Console.Write("Not Accepted\n");
else
Console.Write("Accepted\n");
}
// Driver code
public static void Main(String[] args)
{
String str = "animal";
check(str);
str = "zebra";
check(str);
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
Accepted
Not Accepted
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live