Saturday, January 11, 2025
Google search engine
HomeData Modelling & AIProgram to accept String starting with Capital letter

Program to accept String starting with Capital letter

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 = “neveropen”
Output: Accepted

Input: str = “neveropen”
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++




// C++ program to accept String
// starting with Capital letter
 
#include <iostream>
using namespace std;
 
// Function to check if first
// character is Capital
int checkIfStartsWithCapital(string str)
{
 
    if (str[0] >= 'A' && str[0] <= 'Z')
        return 1;
    else
        return 0;
}
 
// Function to check
void check(string str)
{
    if (checkIfStartsWithCapital(str))
        cout << "Accepted\n";
    else
        cout << "Not Accepted\n";
}
 
// Driver function
int main()
{
    string str = "neveropen";
    check(str);
 
    str = "neveropen";
    check(str);
 
    return 0;
}


Java




// Java program to accept String
// starting with Capital letter
class GFG {
 
    // Function to check if first
    // character is Capital
    static int checkIfStartsWithCapital(String str)
    {
 
        if (str.charAt(0) >= 'A' && str.charAt(0) <= 'Z')
            return 1;
        else
            return 0;
    }
 
    // Function to check
    static void check(String str)
    {
        if (checkIfStartsWithCapital(str) == 1)
            System.out.println("Accepted");
        else
            System.out.println("Not Accepted");
    }
 
    // Driver function
    public static void main(String[] args)
    {
        String str = "neveropen";
        check(str);
 
        str = "neveropen";
        check(str);
    }
}
 
// This code is contributed by AnkitRai01


Python3




# Python3 program to accept String
# starting with Capital letter
 
# Function to check if first
# character is Capital
 
 
def checkIfStartsWithCapital(string):
 
    if (string[0] >= 'A' and string[0] <= 'Z'):
        return 1
    else:
        return 0
 
# Function to check
 
 
def check(string):
 
    if (checkIfStartsWithCapital(string)):
        print("Accepted")
    else:
        print("Not Accepted")
 
 
# Driver function
if __name__ == "__main__":
 
    string = "neveropen"
    check(string)
 
    string = "neveropen"
    check(string)
 
# This code is contributed by AnkitRai01


C#




// C# program to accept String
// starting with Capital letter
using System;
 
class GFG {
 
    // Function to check if first
    // character is Capital
    static int checkIfStartsWithCapital(string str)
    {
 
        if (str[0] >= 'A' && str[0] <= 'Z')
            return 1;
        else
            return 0;
    }
 
    // Function to check
    static void check(string str)
    {
        if (checkIfStartsWithCapital(str) == 1)
            Console.WriteLine("Accepted");
        else
            Console.WriteLine("Not Accepted");
    }
 
    // Driver function
    public static void Main()
    {
        string str = "neveropen";
        check(str);
 
        str = "neveropen";
        check(str);
    }
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
 
 
// JavaScript program to accept String
// starting with Capital letter
 
// Function to check if first
// character is Capital
function checkIfStartsWithCapital(str)
{
 
    if (str[0] >= 'A' && str[0] <= 'Z')
        return 1;
    else
        return 0;
}
 
// Function to check
function check(str)
{
    if (checkIfStartsWithCapital(str))
        document.write( "Accepted<br>");
    else
        document.write( "Not Accepted<br>");
}
 
// Driver function
 
var str = "neveropen";
check(str);
str= "neveropen";
check(str);
 
 
</script>


Output: 

Accepted
Not Accepted

 

Time complexity: O(1)
Auxiliary space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments