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 = "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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 = "GeeksforGeeks";
    check(str);
  
    str = "geeksforgeeks";
    check(str);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 = "GeeksforGeeks"
        check(str); 
      
        str = "geeksforgeeks"
        check(str); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 = "GeeksforGeeks"
    check(string); 
  
    string = "geeksforgeeks"
    check(string); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 = "GeeksforGeeks"
        check(str); 
      
        str = "geeksforgeeks"
        check(str); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

Accepted
Not Accepted

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.