簡単 4件解決済み 正答率100.0%

Count Uppercase Letters

Count the capital letters in a text.

このチャレンジで学べること

  • 文字列の文字をループで調べる
  • 大文字を見分ける
  • 一致した数を数える

問題

Count how many characters in text are uppercase English letters A–Z.

Implement count_uppercase(text).

Input

text: The text to inspect.

Output

Return the number of uppercase letters.

制約

0 ≤ length of text ≤ 10,000 (ASCII characters)

例

例 1
入力:
text = "Hello World"

期待される出力:
expected = 2
入力:
std::string text = "Hello World";

期待される出力:
long long expected = 2;
入力:
string text = "Hello World";

期待される出力:
long expected = 2;

解説

H and W are uppercase: 2.

例 2
入力:
text = "no caps here!"

期待される出力:
expected = 0
入力:
std::string text = "no caps here!";

期待される出力:
long long expected = 0;
入力:
string text = "no caps here!";

期待される出力:
long expected = 0;

解説

There are no uppercase letters: 0.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

文字列を1回走査し、A〜Z だけを数える。

ヒント 2

単語との比較や分割ではなく、各文字の大文字小文字を直接調べる。

ヒント 3

小文字、数字、空白、記号は数えない。

目安の計算量

時間 O(n)、追加メモリ O(1)

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 KKKaneki Ken Python 869 ms - 3/5 コードを見る
2 ฐศฐิติพร ศิลป์ประกอบ Python 878 ms - 3/5 コードを見る
3 LMLaura Mahlow Python 884 ms - 4/5 コードを見る
4 NNNhân Nguyễn Thiện C++ 3,293 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意:下に4件の全解答あり まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。