📌  相关文章
📜  检查字符串中的大写字符(大写字母)是否正确使用 |设置 2

📅  最后修改于: 2022-05-13 01:57:06.679000             🧑  作者: Mango

检查字符串中的大写字符(大写字母)是否正确使用 |设置 2

给定一个由大写和小写字母组成的字符串S ,任务是检查给定字符串中是否正确使用了大写字符。大写字符的正确用法如下:

  • 字符串中的所有字符都是大写的。例如, “极客”
  • 没有一个字符是大写的。例如, “极客”
  • 只有第一个字符是大写的。例如, “极客”

例子:

方法:给定的问题已经在本文的第 1 组中讨论过。本文提出了一种不同且易于实施的方法,该方法基于以下两个观察结果:

  • 如果当前字母是大写字母而前一个字母是小字母,则返回false
  • 如果当前字母是小字母且前一个字母是大写字母且前一个字母不是字符串的一个字符,则返回false

如果遍历了完整的字符串而没有违反以上两种情况,则返回true

下面是上述方法的实现

C++
// C++ program for the above approach
#include 
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



输出
No

时间复杂度: O(N)
辅助空间: O(1)