Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does Java judge whether the use of string uppercase letters is legal?

Shulou Source: shulou.com Published: 2022-06-01 11:24:33 09月21日 Update

This article mainly explains "Java how to judge whether the use of string capital letters is legal", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java how to judge whether the use of string capital letters is legal"!

Topic: judge whether the use of capital letters in a string is legal. Legal conditions: (1) all uppercase letters; (2) all lowercase letters; (3) only the first letter in uppercase and the rest in lowercase.

Train of thought:

A) C++

Initializes a variable that contains the number of uppercase characters in a record string. Traverses each character of the string, and if the character is an uppercase letter, the uppercase letter count variable counts once (plus one).

Legal conditions:

The capital letter count variable is 0 (all lowercase letters)

The capital letter count variable is the string length (all uppercase letters)

The uppercase letter count variable is 1 and the first character of the string is uppercase (only the first letter is uppercase and the rest is lowercase).

B) Python

Python provides API interfaces that can be called directly:

The isupper () method detects whether all letters in the string are uppercase

The islower () method detects whether a string consists of lowercase letters

The istitle () method detects whether the first letter of the spelling of all words in the string is uppercase and the other letters are lowercase.

Code:

Language: cpp

Class Solution {public:bool detectCapitalUse (string word) {int cnt = 0 for (char c: word) {if ('Z'-c > = 0) {cnt++;}} return ((cnt = = 0 | | cnt = = word.length () | (cnt = = 1 & &'Z'-word [0] > = 0));}}

Language: python

Class Solution (object): def detectCapitalUse (self, word): "": type word: str: rtype: bool "return word.isupper () or word.islower () or word.istitle () Thank you for your reading. This is the content of" how Java judges whether the use of string capitals is legal ". After the study of this article, I believe you have a deeper understanding of how Java judges whether the use of string uppercase letters is legal, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Letters uppercase characters uppercase letters strings legal lowercase variables methods learning detection content only ideas conditions codes words that is situations interfaces Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft MySQL Shulou Tech Info Xiaomi