Open In App

Check if uppercase characters in a string are used correctly or not

Last Updated : 28 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S consisting of uppercase and lowercase letters, the task is to check if uppercase characters are used correctly in the given string or not. Correct usage of uppercase characters are as follows:

  • All characters in the string are in uppercase. For example, “GEEKS”.
  • None of the characters are in uppercase. For example, “geeks”.
  • Only the first character is in uppercase. For example, “Geeks”.

Examples:

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

Input: S = “GeeksForGeeks”
Output: No

Approach: Follow the steps below to solve the problem:

  • Check if the first character of the string is in uppercase or not. If found to be true, iterate over the remaining characters.
  • If all the remaining characters are in uppercase, print “Yes”. Otherwise, if any of the remaining characters are in uppercase, then print “NO”.
  • If the first character is not in uppercase, then check if all the remaining characters are in lowercase or not. If found to be true, then print “YES”. Otherwise, print “NO”.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the
// character c is in lowercase or not
bool isLower(char c)
{
 
    return c >= 'a' and c <= 'z';
}
 
// Function to check if the
// character c is in uppercase or not
bool isUpper(char c)
{
 
    return c >= 'A' and c <= 'Z';
}
 
// Utility function to check if uppercase
// characters are used correctly or not
bool detectUppercaseUseUtil(string S)
{
   
    // Length of string
    int N = S.size();
    int i;
 
    // If the first character is in lowercase
    if (isLower(S[0]))
    {
        i = 1;
        while (S[i] && isLower(S[i]))
            ++i;
        return i == N ? true : false;
    }
 
    // Otherwise
    else {
        i = 1;
 
        // Check if all characters
        // are in uppercase or not
        while (S[i] && isUpper(S[i]))
            ++i;
 
        // If all characters are
        // in uppercase
        if (i == N)
            return true;
        else if (i > 1)
            return false;
 
        // Check if all characters except
        // the first are in lowercase
        while (S[i] && isLower(S[i]))
            ++i;
        return i == N ? true : false;
    }
}
 
// Function to check if uppercase
// characters are used correctly or not
void detectUppercaseUse(string S)
{
   
    // Stores whether the use of uppercase
    // characters are correct or not
    bool check = detectUppercaseUseUtil(S);
 
    // If correct
    if (check)
        cout << "Yes";
 
    // Otherwise
    else
        cout << "No";
}
 
// Driver Code
int main()
{
   
    // Given string
    string S = "GeeKs";
 
    // Function call to check if use of
    // uppercase characters is correct or not
    detectUppercaseUse(S);
 
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
 
  // Function to check if the
  // character c is in lowercase or not
  static boolean isLower(char c)
  {
 
    return c >= 'a' && c <= 'z';
  }
 
  // Function to check if the
  // character c is in uppercase or not
  static boolean isUpper(char c)
  {
 
    return c >= 'A' && c <= 'Z';
  }
 
  // Utility function to check if uppercase
  // characters are used correctly or not
  static boolean detectUppercaseUseUtil(String S)
  {
    // Length of string
    int N = S.length();
 
    int i;
 
    // If the first character is in lowercase
    if (isLower(S.charAt(0))) {
      i = 1;
      while (i<N && isLower(S.charAt(i)))
        ++i;
      return i == N ? true : false;
    }
 
    // Otherwise
    else {
      i = 1;
 
      // Check if all characters
      // are in uppercase or not
      while (i<N && isUpper(S.charAt(i)))
        ++i;
 
      // If all characters are
      // in uppercase
      if (i == N)
        return true;
      else if (i > 1)
        return false;
 
      // Check if all characters except
      // the first are in lowercase
      while (i<N && isLower(S.charAt(i)))
        ++i;
      return i == N ? true : false;
    }
  }
 
  // Function to check if uppercase
  // characters are used correctly or not
  static void detectUppercaseUse(String S)
  {
     
    // Stores whether the use of uppercase
    // characters are correct or not
    boolean check = detectUppercaseUseUtil(S);
 
    // If correct
    if (check)
      System.out.println("Yes");
 
    // Otherwise
    else
      System.out.println("No");
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    // Given string
    String S = "GeeKs";
 
    // Function call to check if use of
    // uppercase characters is correct or not
    detectUppercaseUse(S);
  }
}
 
// This code is contributed by subhamsingh10


Python3




# Python3 program for the above approach
 
# Function to check if the
# character c is in lowercase or not
def isLower(c):
    return ord(c) >= ord('a') and ord(c) <= ord('z')
 
# Function to check if the
# character c is in uppercase or not
def isUpper(c):
    return ord(c) >= ord('A') and ord(c) <= ord('Z')
 
# Utility function to check if uppercase
# characters are used correctly or not
def detectUppercaseUseUtil(S):
   
    # Length of string
    N = len(S)
    i = 0
 
    # If the first character is in lowercase
    if (isLower(S[0])):
        i = 1
        while (S[i] and isLower(S[i])):
            i += 1
        return True if (i == N) else False
 
    # Otherwise
    else:
        i = 1
 
        # Check if all characters
        # are in uppercase or not
        while (S[i] and isUpper(S[i])):
            i += 1
 
        # If all characters are
        # in uppercase
        if (i == N):
            return True
        elif (i > 1):
            return False
 
        # Check if all characters except
        # the first are in lowercase
        while (S[i] and isLower(S[i])):
            i += 1
        return True if (i == N) else False
 
# Function to check if uppercase
# characters are used correctly or not
def detectUppercaseUse(S):
   
    # Stores whether the use of uppercase
    # characters are correct or not
    check = detectUppercaseUseUtil(S)
 
    # If correct
    if (check):
        print("Yes")
    # Otherwise
    else:
        print ("No")
 
# Driver Code
if __name__ == '__main__':
   
    # Given string
    S = "GeeKs"
 
    # Function call to check if use of
    # uppercase characters is correct or not
    detectUppercaseUse(S)
 
# This code is contributed by mohit kumar 29.


C#




using System;
public class GFG
{
 
  // Function to check if the
  // character c is in lowercase or not
  static bool isLower(char c)
  {
 
    return c >= 'a' && c <= 'z';
  }
 
  // Function to check if the
  // character c is in uppercase or not
  static bool isUpper(char c)
  {
 
    return c >= 'A' && c <= 'Z';
  }
 
  // Utility function to check if uppercase
  // characters are used correctly or not
  static bool detectUppercaseUseUtil(string S)
  {
 
    // Length of string
    int N = S.Length;
 
    int i;
 
    // If the first character is in lowercase
    if (isLower(S[0]))
    {
      i = 1;
      while (i < N && isLower(S[i]))
        ++i;
      return i == N ? true : false;
    }
 
    // Otherwise
    else {
      i = 1;
 
      // Check if all characters
      // are in uppercase or not
      while (i < N && isUpper(S[i]))
        ++i;
 
      // If all characters are
      // in uppercase
      if (i == N)
        return true;
      else if (i > 1)
        return false;
 
      // Check if all characters except
      // the first are in lowercase
      while (i < N && isLower(S[i]))
        ++i;
      return i == N ? true : false;
    }
  }
 
  // Function to check if uppercase
  // characters are used correctly or not
  static void detectUppercaseUse(string S)
  {
 
    // Stores whether the use of uppercase
    // characters are correct or not
    bool check = detectUppercaseUseUtil(S);
 
    // If correct
    if (check)
      Console.WriteLine("Yes");
 
    // Otherwise
    else
      Console.WriteLine("No");
  }
 
  // Driver Code
  static public void Main ()
  {
     
    // Given string
    string S = "GeeKs";
 
    // Function call to check if use of
    // uppercase characters is correct or not
    detectUppercaseUse(S);
  }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript




<script>
      // JavaScript program for the above approach
      // Function to check if the
      // character c is in lowercase or not
      function isLower(str) {
        return str === str.toLowerCase();
      }
 
      // Function to check if the
      // character c is in uppercase or not
      function isUpper(str) {
        return str === str.toUpperCase();
      }
 
      // Utility function to check if uppercase
      // characters are used correctly or not
      function detectUppercaseUseUtil(S) {
        // Length of string
        var N = S.length;
        var i;
 
        // If the first character is in lowercase
        if (isLower(S[0])) {
          i = 1;
          while (S[i] && isLower(S[i])) ++i;
          return i === N ? true : false;
        }
 
        // Otherwise
        else {
          i = 1;
 
          // Check if all characters
          // are in uppercase or not
          while (S[i] && isUpper(S[i])) ++i;
 
          // If all characters are
          // in uppercase
          if (i === N) return true;
          else if (i > 1) return false;
 
          // Check if all characters except
          // the first are in lowercase
          while (S[i] && isLower(S[i])) ++i;
          return i === N ? true : false;
        }
      }
 
      // Function to check if uppercase
      // characters are used correctly or not
      function detectUppercaseUse(S) {
        // Stores whether the use of uppercase
        // characters are correct or not
        var check = detectUppercaseUseUtil(S);
 
        // If correct
        if (check) document.write("Yes");
        // Otherwise
        else document.write("No");
      }
 
      // Driver Code
      // Given string
      var S = "GeeKs";
      // Function call to check if use of
      // uppercase characters is correct or not
      detectUppercaseUse(S);
    </script>


Output: 

No

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads