Given a string str consisting of alphabets, the task is to check whether the given string is starting with a Capital Letter or Not.
Examples:
Input: str = “GeeksforGeeks”
Output: Accepted
Input: str = “geeksforgeeks”
Output: Not Accepted
Approach:
- Find the ASCII value of the first character of the string
- Check if this value lies in the range of [65, 90] or not
- If yes, print Accepted
- Else print Not Accepted
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int checkIfStartsWithCapital(string str)
{
if (str[0] >= 'A' && str[0] <= 'Z')
return 1;
else
return 0;
}
void check(string str)
{
if (checkIfStartsWithCapital(str))
cout << "Accepted\n";
else
cout << "Not Accepted\n";
}
int main()
{
string str = "GeeksforGeeks";
check(str);
str = "geeksforgeeks";
check(str);
return 0;
}
|
Java
class GFG {
static int checkIfStartsWithCapital(String str)
{
if (str.charAt(0) >= 'A' && str.charAt(0) <= 'Z')
return 1;
else
return 0;
}
static void check(String str)
{
if (checkIfStartsWithCapital(str) == 1)
System.out.println("Accepted");
else
System.out.println("Not Accepted");
}
public static void main(String[] args)
{
String str = "GeeksforGeeks";
check(str);
str = "geeksforgeeks";
check(str);
}
}
|
Python3
def checkIfStartsWithCapital(string):
if (string[0] >= 'A' and string[0] <= 'Z'):
return 1
else:
return 0
def check(string):
if (checkIfStartsWithCapital(string)):
print("Accepted")
else:
print("Not Accepted")
if __name__ == "__main__":
string = "GeeksforGeeks"
check(string)
string = "geeksforgeeks"
check(string)
|
C#
using System;
class GFG {
static int checkIfStartsWithCapital(string str)
{
if (str[0] >= 'A' && str[0] <= 'Z')
return 1;
else
return 0;
}
static void check(string str)
{
if (checkIfStartsWithCapital(str) == 1)
Console.WriteLine("Accepted");
else
Console.WriteLine("Not Accepted");
}
public static void Main()
{
string str = "GeeksforGeeks";
check(str);
str = "geeksforgeeks";
check(str);
}
}
|
Javascript
<script>
function checkIfStartsWithCapital(str)
{
if (str[0] >= 'A' && str[0] <= 'Z')
return 1;
else
return 0;
}
function check(str)
{
if (checkIfStartsWithCapital(str))
document.write( "Accepted<br>");
else
document.write( "Not Accepted<br>");
}
var str = "GeeksforGeeks";
check(str);
str= "geeksforgeeks";
check(str);
</script>
|
Output: Accepted
Not Accepted
Time complexity: O(1)
Auxiliary space: O(1)
Last Updated :
24 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...