Program to accept Strings starting with a Vowel
Last Updated :
13 Sep, 2022
Given string str consisting of alphabets, the task is to check whether the given string is starting with a Vowel or Not.
Examples:
Input: str = "Animal"
Output: Accepted
Input: str = "GeeksforGeeks"
Output: Not Accepted
Approach:
- Find the first character of the string
- Check if the first character of the string is a vowel or not
- If yes, print Accepted
- Else print Not Accepted
Below is the implementation of the above approach:
CPP
#include <iostream>
using namespace std;
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;
}
void check(string str)
{
if (checkIfStartsWithVowels(str))
cout << "Not Accepted\n" ;
else
cout << "Accepted\n" ;
}
int main()
{
string str = "animal" ;
check(str);
str = "zebra" ;
check(str);
return 0;
}
|
Java
class GFG
{
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 ;
}
static void check(String str)
{
if (checkIfStartsWithVowels(str.toCharArray()) == 1 )
System.out.print( "Not Accepted\n" );
else
System.out.print( "Accepted\n" );
}
public static void main(String[] args)
{
String str = "animal" ;
check(str);
str = "zebra" ;
check(str);
}
}
|
Python3
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 ;
def check(string) :
if (checkIfStartsWithVowels(string)) :
print ( "Not Accepted" );
else :
print ( "Accepted" );
if __name__ = = "__main__" :
string = "animal" ;
check(string);
string = "zebra" ;
check(string);
|
C#
using System;
class GFG
{
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;
}
static void check(String str)
{
if (checkIfStartsWithVowels(str.ToCharArray()) == 1)
Console.Write( "Not Accepted\n" );
else
Console.Write( "Accepted\n" );
}
public static void Main(String[] args)
{
String str = "animal" ;
check(str);
str = "zebra" ;
check(str);
}
}
|
Javascript
<script>
function checkIfStartsWithVowels(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 check(str)
{
if (checkIfStartsWithVowels(str))
document.write( "Not Accepted<br>" );
else
document.write( "Accepted<br>" );
}
var str = "animal" ;
check(str);
str = "zebra" ;
check(str);
</script>
|
Output:
Accepted
Not Accepted
Time complexity: O(1) as it is performing constant operations
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...