Open In App

Check string for all uppercase-lowercase or first uppercase and rest lowercase characters

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S, the task is to check if all the characters in S are in lowercase, or all the characters are in uppercase or only the first character is in uppercase, and the remaining characters are in lowercase. Print “YES” if any of the condition gets satisfied else print “NO”.

Examples:

Input: S = “Geeks”
Output: YES
Explanation: Only the first character of S is in uppercase and all the remaining characters are in lowercase.

Input: S = “geeksforgeeks”
Output: YES
Explanation: All the characters in S are in lowercase.

Input: S = “GFG”
Output: YES
Explanation: All the characters in S are in uppercase.

Input: S = “geekS”
Output: NO
Explanation: The string S does not have all uppercase, lowercase or only the first character as uppercase, therefore print “NO”.

Approach: To solve the problem, follow the below idea:

The problem can be solved by counting all the uppercase and lowercase characters in the string S. If the count of uppercase characters is equal to the length of S or the count of lowercase characters is equal to length of S, then print “YES”. Otherwise, if the count of uppercase is 1, count of lowercase characters is (length of S – 1) and the first character of S is in uppercase, then print “YES”. Otherwise print “NO”.

Below is the implementation of the above approach:

C++
#include <bits/stdc++.h>
using namespace std;

// Function to check whether ch is in uppercase
bool isUpperCase(char ch) { return ch >= 'A' && ch <= 'Z'; }

// function to check whether ch is in lowercase
bool isLowerCase(char ch) { return ch >= 'a' && ch <= 'z'; }

// function to check if S has only uppercase or only
// lowercase or only the first character is in uppercase and
// all the remaining characters are in lowercase
string solve(string S)
{
    // Variable to store the length of string S
    int len = S.length();

    // Variable to store the count of lowercase and
    // uppercase letters
    int countLower = 0, countUpper = 0;

    // Count the number of lowercase and uppercase letters
    for (int i = 0; i < len; i++) {
        if (isUpperCase(S[i]))
            countUpper += 1;
        else if (isLowerCase(S[i]))
            countLower += 1;
    }

    // If string S has only uppercase or only lowercase
    // letters
    if (countLower == len || countUpper == len)
        return "YES";

    // If string S has the first character as uppercase and
    // all the remaining characters as lowercase
    if (countUpper == 1 && countLower == len - 1
        && isUpperCase(S[0]))
        return "YES";

    // If no condition satisfies, return "NO"
    return "NO";
}

int main()
{
    // Sample Input
    cout << solve("Geeks") << endl;
    cout << solve("geeksforgeeks") << endl;
    cout << solve("GFG") << endl;
    cout << solve("geekS") << endl;
    return 0;
}
Java
public class CaseCheck {

    // Function to check whether ch is in uppercase
    static boolean isUpperCase(char ch) {
        return ch >= 'A' && ch <= 'Z';
    }

    // Function to check whether ch is in lowercase
    static boolean isLowerCase(char ch) {
        return ch >= 'a' && ch <= 'z';
    }

    // Function to check if S has only uppercase or only
    // lowercase or only the first character is in uppercase and
    // all the remaining characters are in lowercase
    static String solve(String S) {
        // Variable to store the length of string S
        int len = S.length();

        // Variable to store the count of lowercase and
        // uppercase letters
        int countLower = 0, countUpper = 0;

        // Count the number of lowercase and uppercase letters
        for (int i = 0; i < len; i++) {
            if (isUpperCase(S.charAt(i)))
                countUpper += 1;
            else if (isLowerCase(S.charAt(i)))
                countLower += 1;
        }

        // If string S has only uppercase or only lowercase
        // letters
        if (countLower == len || countUpper == len)
            return "YES";

        // If string S has the first character as uppercase and
        // all the remaining characters as lowercase
        if (countUpper == 1 && countLower == len - 1
                && isUpperCase(S.charAt(0)))
            return "YES";

        // If no condition satisfies, return "NO"
        return "NO";
    }

    public static void main(String[] args) {
        // Sample Input
        System.out.println(solve("Geeks"));
        System.out.println(solve("geeksforgeeks"));
        System.out.println(solve("GFG"));
        System.out.println(solve("geekS"));
    }
}

// This code is contributed by shivamgupta0987654321
JavaScript
// Function to check whether ch is in uppercase
function isUpperCase(ch) {
    return ch >= 'A' && ch <= 'Z';
}

// Function to check whether ch is in lowercase
function isLowerCase(ch) {
    return ch >= 'a' && ch <= 'z';
}

// Function to check if S has only uppercase or only lowercase or only the first character is in uppercase and all the remaining characters are in lowercase
function solve(S) {
    // Variable to store the length of string S
    const len = S.length;

    // Variable to store the count of lowercase and uppercase letters
    let countLower = 0, countUpper = 0;

    // Count the number of lowercase and uppercase letters
    for (let i = 0; i < len; i++) {
        if (isUpperCase(S[i]))
            countUpper += 1;
        else if (isLowerCase(S[i]))
            countLower += 1;
    }

    // If string S has only uppercase or only lowercase letters
    if (countLower === len || countUpper === len)
        return "YES";

    // If string S has the first character as uppercase and all the remaining characters as lowercase
    if (countUpper === 1 && countLower === len - 1 && isUpperCase(S[0]))
        return "YES";

    // If no condition satisfies, return "NO"
    return "NO";
}

// Sample Input
console.log(solve("Geeks"));
console.log(solve("geeksforgeeks"));
console.log(solve("GFG"));
console.log(solve("geekS"));
Python3
# Function to check whether ch is in uppercase
def is_upper_case(ch):
    return 'A' <= ch <= 'Z'

# Function to check whether ch is in lowercase
def is_lower_case(ch):
    return 'a' <= ch <= 'z'

# Function to check if S has only uppercase or only
# lowercase or only the first character is in uppercase and
# all the remaining characters are in lowercase
def solve(S):
    # Variable to store the length of string S
    len_S = len(S)

    # Variables to store the count of lowercase and
    # uppercase letters
    count_lower = 0
    count_upper = 0

    # Count the number of lowercase and uppercase letters
    for i in range(len_S):
        if is_upper_case(S[i]):
            count_upper += 1
        elif is_lower_case(S[i]):
            count_lower += 1

    # If string S has only uppercase or only lowercase
    # letters
    if count_lower == len_S or count_upper == len_S:
        return "YES"

    # If string S has the first character as uppercase and
    # all the remaining characters as lowercase
    if count_upper == 1 and count_lower == len_S - 1 and is_upper_case(S[0]):
        return "YES"

    # If no condition satisfies, return "NO"
    return "NO"

# Sample Input
print(solve("Geeks"))
print(solve("geeksforgeeks"))
print(solve("GFG"))
print(solve("geekS"))

Output
YES
YES
YES
NO



Time Complexity: O(N), where N is the length of input string S.
Auxiliary Space: O(1)



Similar Reads

Check if a string contains uppercase, lowercase, special characters and numeric values
Given string str of length N, the task is to check whether the given string contains uppercase alphabets, lowercase alphabets, special characters, and numeric values or not. If the string contains all of them, then print "Yes". Otherwise, print "No". Examples: Input: str = "#GeeksForGeeks123@" Output: Yes Explanation: The given string contains uppe
5 min read
Check if lowercase and uppercase characters are in same order
Given string str containing only lower and uppercase alphabets. The task is to check if both lowercase characters and uppercase characters follow the same order respectively. Note: If a character occurs more than once in lowercase then the occurrence of the same character in the uppercase should be same. Examples: Input: str = "geeGkEEsKS" Output:
4 min read
Remove uppercase, lowercase, special, numeric, and non-numeric characters from a String
Given string str of length N, the task is to remove uppercase, lowercase, special, numeric, and non-numeric characters from this string and print the string after the simultaneous modifications. Examples: Input: str = “GFGgfg123$%” Output: After removing uppercase characters: gfg123$% After removing lowercase characters: GFG123$% After removing spe
10 min read
Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
Given an integer 'k' and a string 'str' consisting of characters from English alphabets. The task is to convert all lower case character to uppercase whose ASCII value is co-prime with k. Examples: Input: str = "geeksforgeeks", k = 4 Output: GEEKSfOrGEEKS 'f' and 'r' are the only characters whose ASCII values aren't co-prime with 4. Input: str = "A
6 min read
Minimum number of operations to move all uppercase characters before all lower case characters
Given a string str, containing upper case and lower case characters. In a single operations, any lowercase character can be converted to an uppercase character and vice versa. The task is to print the minimum number of such operations required so that the resultant string consists of zero or more upper case characters followed by zero or more lower
8 min read
Program to display all alphabets from A to Z in uppercase and lowercase both
Alphabets in lowercase and uppercase can be printed using two methods, first is using ASCII values and second is to directly print values from 'A' to 'Z' using loops. Below are the implementation of both methods: Program to display all alphabets from A to Z in uppercase and lowercase using ASCII values:ASCII value of uppercase alphabets - 65 to 90.
7 min read
How to create half of the string in uppercase and the other half in lowercase?
The first thing that we have to keep in my mind while solving this kind of problem is that the strings are immutable i.e, If I am taking the following string var string1 = "geeksforgeeks";string1[0] = "G";console.log(string1);As strings are immutable, so we cannot change the character of the string, so the output of the above code will be the follo
9 min read
Minimize cost to convert all occurrences of each distinct character to lowercase or uppercase
Given a string S consisting of N alphabets and two positive integers L and U, representing the cost of converting any character to lowercase and uppercase respectively, the task is to modify the string S such that every occurrence of the alphabet in the string S is either lowercase or uppercase and the cost of converting the original string to the
11 min read
Count Fair pairs in a String with Uppercase-Lowercase operations
Given a string S of length N that contains only uppercase and lowercase letters, the task is to find the maximum number of pairs that consist of an uppercase and a lowercase version of the same letter, and each character can only belong to one pair. You are allowed to perform K operations, wherein each operation, you can either change an uppercase
9 min read
Conversion of whole String to uppercase or lowercase using STL in C++
Given a string, convert the whole string to uppercase or lowercase using STL in C++. Examples: For uppercase conversionInput: s = "String"Output: s = "STRING" For lowercase conversionInput: s = "String"Output: s = "string" Functions used : transform : Performs a transformation on given array/string. toupper(char c): Returns the upper case version o
1 min read
Article Tags :
Practice Tags :