Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report