Sunday, November 17, 2024
Google search engine
HomeData Modelling & AICheck if uppercase characters (Capital letters) in a string are used correctly...

Check if uppercase characters (Capital letters) in a string are used correctly or not | Set 2

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, “neveropen”.
  • 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: The given problem has already been discussed in Set 1 of this article. This article suggests a different and easy to implement approach which is based on the following two observations: 

  • If the current letter is a capital alphabet and the previous letter was a small alphabet, return false.
  • If the current letter is a small alphabet and the previous letter is a capital alphabet and the previous letter is not the 1st character of the string, return false.

If the complete string has been traversed without violating any of the above two cases, return true.

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 uppercase
// characters are used correctly or not
bool detectUppercaseUse(string word)
{
    // Loop to iterate through
    // the given string S
    for (int i = 1; i < word.length(); i++) {
 
        // Current character is
        // Capital and previous
        // character is small
        if (word[i] - 'A' < 32
            && word[i - 1] - 'A' >= 32) {
            return false;
        }
 
        // Current character is
        // small and previous is
        // a capital character
        else if (word[i] - 'A' >= 32
                 && word[i - 1] - 'A' < 32) {
 
            // If previous char
            // is the 1st char
            if (i - 1 == 0)
                continue;
 
            return false;
        }
    }
 
    // Return true
    return true;
}
// Driver Code
int main()
{
    string S = "GeeKs";
    cout << (detectUppercaseUse(S) ? "Yes" : "No");
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
  // Function to check if uppercase
  // characters are used correctly or not
  static boolean detectUppercaseUse(char []word)
  {
 
    // Loop to iterate through
    // the given String S
    for (int i = 1; i < word.length; i++) {
 
      // Current character is
      // Capital and previous
      // character is small
      if (word[i] - 'A' < 32
          && word[i - 1] - 'A' >= 32) {
        return false;
      }
 
      // Current character is
      // small and previous is
      // a capital character
      else if (word[i] - 'A' >= 32
               && word[i - 1] - 'A' < 32) {
 
        // If previous char
        // is the 1st char
        if (i - 1 == 0)
          continue;
 
        return false;
      }
    }
 
    // Return true
    return true;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String S = "GeeKs";
    System.out.print(detectUppercaseUse(S.toCharArray()) ? "Yes" : "No");
  }
}
 
// This code is contributed by 29AjayKumar


Python3




# Python code for the above approach
 
# Function to check if uppercase
# characters are used correctly or not
def detectUppercaseUse(word):
 
    # Loop to iterate through
    # the given string S
    for i in range(1, len(word)):
 
        # Current character is
        # Capital and previous
        # character is small
        if (ord(word[i]) - ord('A') < 32 and ord(word[i - 1]) - ord('A') >= 32):
            return False;
         
 
        # Current character is
        # small and previous is
        # a capital character
        elif (ord(word[i]) - ord('A') >= 32 and ord(word[i - 1]) - ord('A') < 32):
 
            # If previous char
            # is the 1st char
            if (i - 1 == 0):
                continue;
 
            return False;
         
 
    # Return true
    return True;
 
 
# Driver Code
S = "GeeKs";
print("Yes" if detectUppercaseUse(S) else "No");
 
# This code is contributed by Saurabh Jaiswal


C#




// C# program for the above approach
using System;
class GFG
{
 
  // Function to check if uppercase
  // characters are used correctly or not
  static bool detectUppercaseUse(string word)
  {
 
    // Loop to iterate through
    // the given string S
    for (int i = 1; i < word.Length; i++) {
 
      // Current character is
      // Capital and previous
      // character is small
      if (word[i] - 'A' < 32
          && word[i - 1] - 'A' >= 32) {
        return false;
      }
 
      // Current character is
      // small and previous is
      // a capital character
      else if (word[i] - 'A' >= 32
               && word[i - 1] - 'A' < 32) {
 
        // If previous char
        // is the 1st char
        if (i - 1 == 0)
          continue;
 
        return false;
      }
    }
 
    // Return true
    return true;
  }
   
  // Driver Code
  public static void Main()
  {
    string S = "GeeKs";
    Console.Write((detectUppercaseUse(S) ? "Yes" : "No"));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
    // JavaScript code for the above approach
 
    // Function to check if uppercase
    // characters are used correctly or not
    function detectUppercaseUse(word)
    {
     
        // Loop to iterate through
        // the given string S
        for (let i = 1; i < word.length; i++) {
 
            // Current character is
            // Capital and previous
            // character is small
            if (word[i].charCodeAt(0) - 'A'.charCodeAt(0) < 32
                && word[i - 1].charCodeAt(0) - 'A'.charCodeAt(0) >= 32) {
                return false;
            }
 
            // Current character is
            // small and previous is
            // a capital character
            else if (word[i].charCodeAt(0) - 'A'.charCodeAt(0) >= 32
                && word[i - 1].charCodeAt(0) - 'A'.charCodeAt(0) < 32) {
 
                // If previous char
                // is the 1st char
                if (i - 1 == 0)
                    continue;
 
                return false;
            }
        }
 
        // Return true
        return true;
    }
     
    // Driver Code
    let S = "GeeKs";
    document.write(detectUppercaseUse(S) ? "Yes" : "No");
 
   // This code is contributed by Potta Lokesh
</script>


 
 

Output

No

 

Time Complexity: O(N)
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